Authentication

import { Callout } from 'nextra/components'

Authentication

The Gett B2C API uses OAuth 2.0 client credentials flow for authentication. You'll need to obtain a bearer token before making any API requests.

Bearer tokens have a limited lifespan. Implement token refresh logic in your application to handle expired tokens gracefully.

Obtaining a Bearer Token

Endpoint

POST https://api.gett.com/v1/oauth/token

Headers

Content-Type: application/x-www-form-urlencoded

Parameters

ParameterTypeRequiredDescription
client_idstringYesThe client ID provided by Gett
client_secretstringYesThe client secret provided by Gett
grant_typestringYesMust be client_credentials
scopestringYesMust be demand_partner

Example Request

curl --request POST 'https://api.gett.com/v1/oauth/token' \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --data-urlencode 'client_id=your_client_id_here' \
  --data-urlencode 'client_secret=your_client_secret_here' \
  --data-urlencode 'grant_type=client_credentials' \
  --data-urlencode 'scope=demand_partner'

Example Response

{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "demand_partner"
}

Using the Bearer Token

Include the bearer token in the Authorization header of all subsequent API requests:

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

Example Authenticated Request

curl --request GET 'https://api.gett.com/v1/private/orders/status/12345' \
  --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...' \
  --header 'Content-Type: application/json'

Token Expiration

Tokens expire after the time specified in the expires_in field (typically 3600 seconds or 1 hour). When a token expires, you'll receive a 401 Unauthorized response.

Handling Expired Tokens

  1. Monitor the expires_in value from the token response
  2. Implement automatic token refresh before expiration
  3. Handle 401 responses by refreshing the token and retrying the request

Example Token Refresh Logic

class GettAPIClient {
  constructor(clientId, clientSecret) {
    this.clientId = clientId;
    this.clientSecret = clientSecret;
    this.token = null;
    this.tokenExpiry = null;
  }

  async getToken() {
    if (this.token && this.tokenExpiry > Date.now()) {
      return this.token;
    }

    const response = await fetch('https://api.gett.com/v1/oauth/token', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      body: new URLSearchParams({
        client_id: this.clientId,
        client_secret: this.clientSecret,
        grant_type: 'client_credentials',
        scope: 'demand_partner'
      })
    });

    const data = await response.json();
    this.token = data.access_token;
    this.tokenExpiry = Date.now() + (data.expires_in * 1000) - 60000; // Refresh 1 minute early

    return this.token;
  }
}

Security Best Practices

**Never expose your client credentials in client-side code or public repositories.**
  • Store client credentials securely (environment variables, secure key management)
  • Use HTTPS for all API communications
  • Implement proper error handling for authentication failures
  • Consider implementing token caching to reduce authentication requests
  • Rotate credentials periodically as part of your security practices

Troubleshooting

Common Authentication Errors

ErrorDescriptionSolution
invalid_clientInvalid client credentialsVerify your client_id and client_secret
invalid_scopeInvalid scope parameterEnsure scope is set to demand_partner
invalid_grantInvalid grant typeEnsure grant_type is client_credentials
401 UnauthorizedToken expired or invalidRefresh your token and retry the request