Skip to main content

Vend access token

POST /api/tokens/vend
Retrieves a usable access token for a user’s connected provider. Handles automatic refresh when tokens are expired.

Request body

{
  "userId": "user-123",
  "provider": "github"
}
FieldTypeRequiredDescription
userIdstringYesThe user requesting the token
providerstringYesThe OAuth provider to vend a token for

Success response (200)

{
  "accessToken": "gho_xxxxxxxxxxxx",
  "expiresAt": "2025-01-15T12:00:00Z",
  "refreshed": false
}
FieldTypeDescription
accessTokenstringThe usable access token
expiresAtstringISO 8601 expiry time (if known)
refreshedbooleanWhether the token was refreshed during this request

Error responses

Provider not linked (404):
{
  "error": "Provider not linked",
  "code": "NOT_LINKED"
}
Token expired with no refresh token (401):
{
  "error": "Token expired",
  "code": "TOKEN_EXPIRED"
}
Token revoked by user (401):
{
  "error": "Token revoked",
  "code": "TOKEN_REVOKED"
}
Refresh failed (502):
{
  "error": "Token refresh failed",
  "code": "REFRESH_FAILED"
}

Behavior

  1. Looks up encrypted tokens for the user + provider
  2. Decrypts the access token
  3. If expired and a refresh token exists, attempts refresh
  4. If refresh fails with invalid_grant, cleans up stored tokens and returns TOKEN_REVOKED
  5. Returns the usable access token