Get usage aggregation
Get usage aggregation
curl --request GET \
--url https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type} \
--header 'Authorization: Bearer <token>'import requests
url = "https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type}"
headers = {"Authorization": "Bearer <token>"}
response = requests.get(url, headers=headers)
print(response.text)const options = {method: 'GET', headers: {Authorization: 'Bearer <token>'}};
fetch('https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type}', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type}"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.get("https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type}")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type}")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"data": [
{
"bucket_time": "2023-11-07T05:31:56Z",
"model_id": "<string>",
"project_id": "<string>",
"key": "<string>",
"prompt_tokens": 1,
"completion_tokens": 1,
"cached_tokens": 1,
"request_count": 123,
"cache_write_5m_tokens": 1,
"cache_write_1h_tokens": 1
}
],
"has_more": true,
"next": "<string>"
}{
"error": {
"type": "invalid_request_error",
"code": "invalid_request",
"message": "<string>",
"request_id": "<string>",
"param": "<string>"
}
}{
"error": {
"type": "invalid_request_error",
"code": "invalid_request",
"message": "<string>",
"request_id": "<string>",
"param": "<string>"
}
}{
"error": {
"type": "invalid_request_error",
"code": "invalid_request",
"message": "<string>",
"request_id": "<string>",
"param": "<string>"
}
}{
"error": {
"type": "invalid_request_error",
"code": "invalid_request",
"message": "<string>",
"request_id": "<string>",
"param": "<string>"
}
}{
"error": {
"type": "invalid_request_error",
"code": "invalid_request",
"message": "<string>",
"request_id": "<string>",
"param": "<string>"
}
}{
"error": {
"type": "invalid_request_error",
"code": "invalid_request",
"message": "<string>",
"request_id": "<string>",
"param": "<string>"
}
}billing:read scope.Window caps depend on granularity: 1m up to 24 hours, 1h up to 31 days, 1d up to 93 days. uid[] is a filter dimension (only rows of keys owned by these users are returned); rows are grouped by bucket × model × project × key.Granularity, alignment, and empty buckets
| Granularity | Bucket alignment | Window cap per query | Empty buckets |
|---|---|---|---|
1m | Rounded down to the minute (Asia/Shanghai) | ≤24 hours | No rows returned for buckets without data |
1h | Rounded down to the hour (Asia/Shanghai) | ≤31 days | Same as above |
1d | Calendar days (Asia/Shanghai) | ≤93 days | Same as above |
Authorizations
Send the Admin API secret as a Bearer token in the Authorization header (Bearer sk-admin-***). Create Admin Keys under Console → Organization → Admin API Keys.
Path Parameters
Usage type. Only completions is supported currently.
completions Query Parameters
Start time in milliseconds, inclusive, e.g. 1790784000000 (2026-10-01 00:00 CST).
x >= 0End time in milliseconds, exclusive, e.g. 1792598400000 (2026-10-22 00:00 CST).
x >= 0Project IDs. Every project must belong to the authenticated organization.
Business API key IDs. Every key must belong to the authenticated organization.
User ID filters. Rows are restricted to these users within the organization.
Time bucket granularity. Window caps differ by granularity: 1m allows up to 24 hours, 1h up to 31 days, 1d up to 93 days; 1d buckets are cut on Asia/Shanghai natural days.
1m, 1h, 1d Page size. Defaults to 100, maximum 1000.
1 <= x <= 1000Cursor from the previous response next field. The cursor carries only the page position; all query parameters must be restated on every page. Keep them unchanged while paginating (except limit) — the server does not check them for consistency.
Response
Usage aggregation rows, ascending by bucket time.
Was this page helpful?
curl --request GET \
--url https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type} \
--header 'Authorization: Bearer <token>'import requests
url = "https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type}"
headers = {"Authorization": "Bearer <token>"}
response = requests.get(url, headers=headers)
print(response.text)const options = {method: 'GET', headers: {Authorization: 'Bearer <token>'}};
fetch('https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type}', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type}"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.get("https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type}")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.moonshot.ai/v1/admin/organizations/usage/{usage_type}")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"data": [
{
"bucket_time": "2023-11-07T05:31:56Z",
"model_id": "<string>",
"project_id": "<string>",
"key": "<string>",
"prompt_tokens": 1,
"completion_tokens": 1,
"cached_tokens": 1,
"request_count": 123,
"cache_write_5m_tokens": 1,
"cache_write_1h_tokens": 1
}
],
"has_more": true,
"next": "<string>"
}{
"error": {
"type": "invalid_request_error",
"code": "invalid_request",
"message": "<string>",
"request_id": "<string>",
"param": "<string>"
}
}{
"error": {
"type": "invalid_request_error",
"code": "invalid_request",
"message": "<string>",
"request_id": "<string>",
"param": "<string>"
}
}{
"error": {
"type": "invalid_request_error",
"code": "invalid_request",
"message": "<string>",
"request_id": "<string>",
"param": "<string>"
}
}{
"error": {
"type": "invalid_request_error",
"code": "invalid_request",
"message": "<string>",
"request_id": "<string>",
"param": "<string>"
}
}{
"error": {
"type": "invalid_request_error",
"code": "invalid_request",
"message": "<string>",
"request_id": "<string>",
"param": "<string>"
}
}{
"error": {
"type": "invalid_request_error",
"code": "invalid_request",
"message": "<string>",
"request_id": "<string>",
"param": "<string>"
}
}