API BCE OAuth2 bearer tokens for server-to-server calls
API BCE OAuth2 tokens: get a bearer token with the client_credentials grant. The /oauth/token request, token lifetime, scopes and examples.
Endpoints for the OAuth tokens resource. All calls require authentication and return the standard JSON envelope.
Base URL: https://companybelgium.be/api · Authentication
Obtain an access token#
OAuth2 client_credentials grant for developer applications. Returns a bearer token usable as an alternative to the X-API-Key / X-API-Secret headers.
Form body
grant_type=client_credentials
client_id=app_xxx
client_secret=secret_xxxRequest
curl -X POST https://companybelgium.be/api/oauth/token \
-d grant_type=client_credentials \
-d client_id=app_xxx \
-d client_secret=secret_xxxResponse
{
"access_token": "cbat_...",
"token_type": "Bearer",
"expires_in": 3600,
"scope": "..."
}When OAuth2 suits an API BCE integration#
The client_credentials grant serves developer applications that talk to the API without a user. The application exchanges its client_id and client_secret for a bearer token, then sends that token on each call. Key headers remain simpler for a single backend. However, OAuth2 fits teams whose tooling already expects bearer tokens.
- Cache the token and reuse it until shortly before
expires_inruns out. - Do not request a new token for every call, since that doubles your HTTP traffic.
- On a 401, fetch a new token once, retry once, then raise an alert.
- Keep
client_secreton the server, exactly like an API secret. - Read the
scopefield to know which data the token may access.
In addition, decide early which applications use OAuth2 and which use key headers. Then, document that choice, because mixed setups confuse new team members during incidents.
Token request details#
The token endpoint sits at https://companybelgium.be/api/oauth/token, outside the v2 path. The body is form-encoded with grant_type=client_credentials, the client id and the client secret. In return, you receive access_token, token_type (Bearer), expires_in and scope. Then, you send Authorization: Bearer followed by the token instead of the two key headers.
This response does not use the standard envelope. Therefore, parse it with its own small function rather than with your envelope parser.
Pitfalls with OAuth2 tokens#
A common error is to send a JSON body to the token endpoint. Instead, use application/x-www-form-urlencoded. Another one is to build the token URL from the v2 base. In addition, the contact routes require the enterprise:contact scope, so a token without it receives an error there.
Running OAuth2 across several services#
When several processes share one application, token handling needs care. First, store the token in a shared cache, so each worker does not request its own. Then, refresh it from a single place, for instance a small job that runs before expiry. Otherwise, many workers may ask for a new token at the same moment. In addition, log the outcome of each token request without the token itself. As a result, you can investigate a 401 without exposing credentials. Finally, document which scopes each application needs.
API BCE OAuth2 FAQ#
How long is an API BCE token valid?
The expires_in field gives the lifetime in seconds. In the reference example, it is 3600.
Do I still need X-API-Key with a token?
No. The bearer token replaces the X-API-Key and X-API-Secret headers on each request.
Why can my token not read contact details?
The contact endpoints require the enterprise:contact scope. So, check the scope returned with your token.