API BCE authentication: API key or OAuth2 bearer token
API BCE authentication: send the X-API-Key and X-API-Secret headers, or an OAuth2 client_credentials token. Ready-to-copy examples and best practices.
API BCE supports two authentication methods. Most integrations use API key headers; developer applications can use OAuth2 instead.
API key + secret (recommended)#
Send both headers on every request:
X-API-Key: your public key identifierX-API-Secret: your private secret
curl https://companybelgium.be/api/v2/enterprise/1033.022.383 \
-H "X-API-Key: $API_BCE_KEY" \
-H "X-API-Secret: $API_BCE_SECRET"Never expose your API secret in client-side code, mobile apps or public repositories. Call the API from your backend.
OAuth2 (developer apps)#
Developer applications can exchange a client_id and client_secret for a short-lived bearer token using the client_credentials grant.
curl -X POST https://companybelgium.be/api/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d grant_type=client_credentials \
-d client_id=$CLIENT_ID \
-d client_secret=$CLIENT_SECRETThen send the token in the Authorization header instead of the API key headers:
curl https://companybelgium.be/api/v2/enterprise/1033.022.383 \
-H "Authorization: Bearer $ACCESS_TOKEN"See the OAuth tokens reference for token lifetime and scopes.
Choosing between key headers and OAuth2#
Both methods reach the same endpoints. However, they suit different situations. Key headers are the simplest choice for a backend that calls API BCE on its own behalf. OAuth2 client_credentials fits developer applications that exchange a client id and secret for a short-lived bearer token. In that case, the token replaces the two headers on each call.
Protect your API BCE credentials in production#
- Keep the secret on the server, never in mobile apps, browser code or public repositories.
- Load credentials from environment variables or from a secrets manager.
- Use test keys (
pk_test_,sk_test_) in development and live keys only in production. - Give each service its own key when your plan allows it: Essentiel includes 2 keys and Pro includes 5.
- Mask both headers in logs, traces and error reports.
Separate keys also make usage easier to read. Because limits are enforced per API key, a batch job on its own key shows its own consumption in /me. As a result, you can see at once which service uses most of your quota.
Troubleshooting authentication errors#
A 401 means missing or invalid credentials. First, check that both headers are present and correctly spelled. Then, make sure you are not sending a test key to the live API, or the reverse: the test API refuses live keys. A 403 is different, because the key is valid but not allowed to access the resource. For example, the test number 0999.999.724 returns 403 on /financial and /roles to simulate a plan upgrade. In addition, OAuth applications need the enterprise:contact scope for the contact endpoints.
Finally, never retry a 401 in a loop. Instead, raise an alert, because the cause is a configuration problem and not a temporary failure.
Rotating credentials also deserves a plan. First, generate the new key in the developer portal. Then, deploy it to every service and check each one with a call to /me. Only after that, stop using the old key. In addition, keep a short written procedure, so anyone on call can repeat it calmly.
API BCE authentication FAQ#
Do I need both X-API-Key and X-API-Secret?
Yes, on every request. The only exception is OAuth2: in that case, you send the bearer token in the Authorization header instead of the two key headers.
How can I check which key a request uses?
Call /me. It returns the key, its company, the plan and the usage of the current period. Also, this call does not count against your quota.
What happens when an OAuth token expires?
Request a new one from the token endpoint with the same grant. The expires_in field gives the lifetime, so you can renew the token before it runs out.