Authentication
How to authenticate with the CNPJá API using an API key in the Authorization header, rotate and revoke keys without downtime and fix 401 errors.
Every request to the API carries an API key in the Authorization header, and the key identifies the organization making the request. The exception is keyless requests to Lookup CNPJ and Lookup CEP, described in Public API access, no key. The full technical specification is under Authentication, in the Reference.
What a key is and how to create, rename and revoke it on the platform are covered in Managing API keys. This page covers the technical side: how to send the key, how to rotate it without stopping the application and how to fix authentication errors.
Sending the key
Send the key as the entire value of the Authorization header, in HTTPS requests to https:/:
GET /v2/establishments/37335118000180 HTTP/1.1
Host: api.cnpja.com
Authorization: YOUR_API_KEY The whole header value is treated as the key, with no authentication scheme:
Authorization value | Result |
|---|---|
YOUR_ | Accepted |
Bearer YOUR_ | 401 API_ |
| Incomplete key, key in quotes or with altered characters | 401 API_ |
| Empty or missing | 401 API_, or a public request on operations that accept one |
- The header name is case-insensitive, but the key, 32 alphanumeric characters long, is case-sensitive.
- The key always goes in the header. The API does not accept the key as a query parameter or in the request body.
- When the header carries a key, the request is never treated as public: the key is always validated, and an invalid or revoked key gets
401instead of falling back to public access. With a valid key, the request accepts every parameter, is billed and counts toward the organization’s limits.
With curl, read the key from an environment variable:
curl https://api.cnpja.com/v2/establishments/37335118000180 \
-H "Authorization: $CNPJA_API_KEY" In a backend, do the same instead of writing the key in the code:
const response = await fetch('https://api.cnpja.com/v2/establishments/37335118000180', {
headers: { Authorization: process.env.CNPJA_API_KEY },
}); Rotation and revocation without downtime
Revocation takes effect immediately and cannot be undone. After it:
- Requests with the key get
401API_, with the revocation date in theKEY_ REVOKED message. Retrying does not help: use another active key of the organization. - A revoked key does not fall back to public access, not even on Lookup CNPJ or Lookup CEP.
To replace a key without interrupting the application:
- Create a new key, as described in Create a key. If the organization is already at its active key limit, first revoke one that is not in use.
- Update the application with the new key, in the environment variable or secrets manager, and restart it if it only reads the key on startup.
- In Settings > Requests, filter by the old key under Actor and confirm that it no longer appears on new requests.
- Only then revoke the old key, as described in Revoke.
If the key leaked, for example to a repository, a log or a message, reverse the order: revoke it immediately, even if the application loses access until it receives the new key.
Best practices
Treat the key like a password: whoever holds it queries data and consumes the organization’s quota and, when set, its overage budget.
- Server-side storage: keep the key only on the server, in environment variables or a secrets manager.
- Out of code and logs: keep the key out of source code, version control, logs, error messages and monitoring tools that record headers.
- No frontend exposure: do not include the key in web pages or mobile apps, where any user can extract it. Call the API from the backend and hand the frontend only the data it needs.
- One key per application: create a key for each application or environment, such as production and staging, and name it accordingly. This way you can tell each key’s usage apart and revoke an exposed key without stopping the others.
- Usage review: follow each key’s usage in Settings > Requests and revoke the keys that no longer appear.
- Immediate revocation: revoke an exposed key without delay, as described in Rotation and revocation without downtime.
Frequent errors
Authentication failures get 401, are not billed, and the code states the reason. Example response for an invalid key:
{
"status": 401,
"code": "API_KEY_INVALID",
"traceId": "4bf92f3577b34da6a3ce929d0e0e4736",
"message": "API key invalid"
} | Status | Code | Cause | Action |
|---|---|---|---|
401 | API_ | Authorization header missing or empty, such as an environment variable with no value | Check that the key reaches the header |
401 | API_ | Request without a key that has query parameters, on an operation with public access. The message names the parameter | Send a key or remove the parameters, as explained in Public API access, no key |
401 | API_ | Bearer or another prefix before the key, incomplete key, or a value that matches no key | Send only the full key as the Authorization value |
401 | API_ | The key was revoked. The message includes the revocation date | Use another active key of the organization or create a new one |
Retrying the same request does not fix a 401: correct the key or the request before trying again. The error format and the other codes are in Limits and errors.
Managing keys on the platform
Owners, Administrators and Developers create, rename and revoke keys in Settings > API keys. The step-by-step guide and the active key limit are in Managing API keys, and what each role can do, in Permissions by role.
Notes
- Authentication is the same on every operation. Each operation’s documentation in the Reference states whether it also accepts public access.
- Removing a member from the organization does not revoke the keys they created. When someone with access to the keys leaves the team, consider rotating them, as explained in Keys of members who leave.
- Responses carry no headers with the request’s usage. Each key’s usage shows up in Settings > Requests, as described in Track usage.