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/summaryRequest Parameters
Section titled “Request Parameters”| Parameter | Type | Description |
|---|---|---|
startDate | number | (Required) Timestamp in milliseconds to specify the start date for usage summary. |
Headers
Section titled “Headers”| Header | Description |
|---|---|
Content-Type | Must be set to application/json |
Authorization | Bearer token for API key |
x-user-id | User ID associated with the API key |
Example Request
Section titled “Example Request”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);}