The Apiera API uses OAuth 2.0 with the client credentials flow. All requests must include a valid access token.
To access the API, you need:
- Client credentials (client ID and secret)
- Organization ID
- Access token (obtained using your credentials)
- Log in to your Apiera Dashboard.
- Navigate to Settings > API Access.
- Create a new API client.
- Save your Client ID, Client Secret, and Organization ID.
Store your client secret securely. It cannot be retrieved after creation.
Request an access token from the Apiera authorization server using your client credentials:
POST /oauth/token HTTP/1.1
Host: auth.apiera.io
Content-Type: application/json
{
"client_id": "YOUR_CLIENT_ID",
"client_secret": "YOUR_CLIENT_SECRET",
"audience": "https://api.apiera.io",
"grant_type": "client_credentials",
"organization": "org_YOUR_ORG_ID"
}Response:
{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "Bearer",
"expires_in": 86400
}Include the access token in the Authorization header on every request:
GET /v1/products HTTP/1.1
Host: api.apiera.io
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...Access tokens expire after 24 hours (expires_in: 86400 seconds). When a token expires, the API returns a 401 Unauthorized response. Request a new token using your client credentials and retry the failed request.
You must cache access tokens and reuse them until they expire. Do not request a new token for every API call. Excessive token requests are treated as abuse and may result in your client credentials being temporarily suspended.
The recommended approach is to cache the token using the expires_in value from the response as the TTL, with a small safety buffer to avoid using a token that is about to expire:
- Request a token.
- Cache the token with a TTL of
expires_in - 60seconds. - On subsequent requests, return the cached token.
- When the cache expires, request a new token.
This ensures you make exactly one token request per expiry cycle, regardless of how many API calls you make.
C# example using in-memory cache:
public sealed class CachedTokenProvider
{
private readonly SemaphoreSlim _semaphore = new(1, 1);
private string? _cachedToken;
private DateTimeOffset _expiresAt;
public async Task<string> GetTokenAsync(CancellationToken cancellationToken = default)
{
if (_cachedToken is not null && DateTimeOffset.UtcNow < _expiresAt)
return _cachedToken;
await _semaphore.WaitAsync(cancellationToken);
try
{
// Double-check after acquiring lock
if (_cachedToken is not null && DateTimeOffset.UtcNow < _expiresAt)
return _cachedToken;
var response = await RequestTokenAsync(cancellationToken);
_cachedToken = response.AccessToken;
_expiresAt = DateTimeOffset.UtcNow.AddSeconds(response.ExpiresIn - 60);
return _cachedToken;
}
finally
{
_semaphore.Release();
}
}
}Key details:
- 60-second buffer prevents using a token right as it expires.
- Semaphore ensures concurrent requests don't trigger multiple token requests simultaneously.
- If you use a distributed cache (Redis, etc.), use the same TTL strategy so all instances of your application share a single cached token.
Secure credentials. Store client secrets in environment variables or a secrets manager, never in source code.
Use separate credentials for development, staging, and production environments.
| Status | Cause |
|---|---|
| 401 Unauthorized | Missing, invalid, or expired token. |
| 403 Forbidden | Valid token but insufficient permissions. |