Skip to main content

Overview

The Usage service provides access to API usage metrics. Check your current billing period’s usage and retrieve historical usage data broken down by day.

Current

Get API usage metrics for the current billing period.
usage, err := client.Usage.Current(ctx)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Developer: %s\n", usage.DeveloperID)
fmt.Printf("Period: %s\n", usage.Period)
fmt.Printf("Token exchanges: %d\n", usage.TokenExchanges)
fmt.Printf("Authorizations: %d\n", usage.Authorizations)
fmt.Printf("Verifications: %d\n", usage.Verifications)
fmt.Printf("Total requests: %d\n", usage.TotalRequests)

Response (UsageMetrics)

FieldTypeDescription
DeveloperIDstringThe developer organization ID.
PeriodstringThe current billing period (e.g., "2026-03").
TokenExchangesintNumber of token exchange operations this period.
AuthorizationsintNumber of authorization requests this period.
VerificationsintNumber of token verifications this period.
TotalRequestsintTotal API requests across all endpoints this period.

History

Retrieve historical usage data for the specified number of days, broken down by day.
history, err := client.Usage.History(ctx, 30)
if err != nil {
    log.Fatal(err)
}
for _, entry := range history.Entries {
    fmt.Printf("%s: %d requests\n", entry.Date, entry.TotalRequests)
    fmt.Printf("  Exchanges: %d\n", entry.TokenExchanges)
    fmt.Printf("  Authorizations: %d\n", entry.Authorizations)
    fmt.Printf("  Verifications: %d\n", entry.Verifications)
}

Parameters

ParameterTypeRequiredDescription
daysintYesNumber of days of history to retrieve (e.g., 7, 30, 90).

Response (UsageHistory)

FieldTypeDescription
Entries[]UsageEntryDaily usage entries, ordered from oldest to newest.

UsageEntry

FieldTypeDescription
DatestringThe date in YYYY-MM-DD format.
TokenExchangesintNumber of token exchange operations on this day.
AuthorizationsintNumber of authorization requests on this day.
VerificationsintNumber of token verifications on this day.
TotalRequestsintTotal API requests on this day.

Full Example

package main

import (
    "context"
    "fmt"
    "log"

    grantex "github.com/mishrasanjeev/grantex-go"
)

func main() {
    client := grantex.NewClient("gx_live_...")
    ctx := context.Background()

    // Check current period usage
    current, err := client.Usage.Current(ctx)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Period: %s\n", current.Period)
    fmt.Printf("Total requests: %d\n", current.TotalRequests)
    fmt.Printf("  Authorizations: %d\n", current.Authorizations)
    fmt.Printf("  Token exchanges: %d\n", current.TokenExchanges)
    fmt.Printf("  Verifications: %d\n", current.Verifications)

    // Get last 30 days of usage
    history, err := client.Usage.History(ctx, 30)
    if err != nil {
        log.Fatal(err)
    }
    totalMonth := 0
    for _, e := range history.Entries {
        totalMonth += e.TotalRequests
    }
    fmt.Printf("\nLast 30 days: %d total requests\n", totalMonth)
}