Skip to content

Get Usage Summary

The getUsageSummary endpoint allows you to retrieve total token usage for a specified date range. This is useful for tracking consumption, budgeting, and understanding API usage patterns.

import { PromptTK } from "@devvle/ptk-api-sdk";
const promptTK = new PromptTK({
userId: "your-user-id",
apiKey: "your-api-key",
});
async function getUsage() {
// Get usage from last 30 days
const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
try {
const response = await promptTK.getUsageSummary(thirtyDaysAgo.getTime()); // Timestamp in milliseconds
console.log("Tokens used:", response.data.tokensUsed);
} catch (error) {
console.error("Error getting usage:", error);
}
}
getUsage();

Expected Output:

{
success: true,
message: 'Usage summary retrieved successfully',
data: {
tokensUsed: 12345 // Total tokens used in the specified date range
}
}

The getUsageSummary function makes a request to the following endpoint:

POST /v1/usage/summary
ParameterTypeDescription
startDatenumber(Required) Timestamp in milliseconds to specify the start date for usage summary.
HeaderDescription
Content-TypeMust be set to application/json
AuthorizationBearer token for API key
x-user-idUser ID associated with the API key
try {
const response = await fetch(`${API_ENDPOINT}/v1/usage/summary`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
"x-user-id": userId,
},
body: JSON.stringify({
startDate: startDate, // Timestamp in milliseconds
}),
});
const data = await response.json();
console.log("Usage Summary:", data);
} catch (error) {
console.error("Error fetching usage summary:", error);
}