Skip to main content

Overview

The Tokens service handles the complete token lifecycle: exchanging authorization codes, refreshing tokens, online verification, and revocation.

Exchange

Exchange an authorization code for a grant token after the user consents.
tokenResp, err := client.Tokens.Exchange(ctx, grantex.ExchangeTokenParams{
    Code:    "authorization-code",
    AgentID: "agent-id",
})

Parameters

ParameterTypeRequiredDescription
CodestringYesAuthorization code from callback
AgentIDstringYesAgent that requested authorization
CodeVerifierstringNoPKCE code verifier (required if code challenge was sent)

Response (ExchangeTokenResponse)

FieldTypeDescription
GrantTokenstringJWT grant token
ExpiresAtstringISO 8601 token expiry
Scopes[]stringGranted scopes
RefreshTokenstringRefresh token for rotation
GrantIDstringGrant record ID

Refresh

Exchange a refresh token for a new grant token. Refresh tokens are single-use — each refresh returns a new refresh token.
tokenResp, err := client.Tokens.Refresh(ctx, grantex.RefreshTokenParams{
    RefreshToken: "current-refresh-token",
    AgentID:      "agent-id",
})
// tokenResp.RefreshToken is a NEW refresh token — store it
Refresh tokens are single-use. Each call returns a new RefreshToken that you must store. The old refresh token is invalidated immediately.

Parameters

ParameterTypeRequiredDescription
RefreshTokenstringYesCurrent refresh token
AgentIDstringYesAgent ID

Response

Same ExchangeTokenResponse as Exchange — includes a new GrantToken and RefreshToken.

Verify

Perform online token verification against the Grantex API.
result, err := client.Tokens.Verify(ctx, "grant-token-jwt")
if err != nil {
    log.Fatal(err)
}
if result.Valid {
    fmt.Printf("Grant ID: %s, Scopes: %v\n", *result.GrantID, result.Scopes)
}

Response (VerifyTokenResponse)

FieldTypeDescription
ValidboolWhether the token is valid
GrantID*stringGrant ID (if valid)
Scopes[]stringToken scopes (if valid)
Principal*stringPrincipal ID (if valid)
Agent*stringAgent DID (if valid)
ExpiresAt*stringToken expiry (if valid)

Revoke

Revoke a token by its JTI (token ID).
err := client.Tokens.Revoke(ctx, "token-jti")
Returns nil on success (HTTP 204).