API keys: Common errors
Solutions for 401 errors, incorrectly sent credentials and lost API keys.
An API request returns 401 or Unauthorized
The server could not authenticate your request with a valid API key.
Check the following first:
- Is the API key complete?
- Does it accidentally contain spaces or line breaks?
- Was the key deleted or replaced by a new one?
- Is the key being sent in the right place?
- Is the header written correctly?
To authenticate via the header, use this format:
Authorization: Bearer YOUR_API_KEY
Example as JSON:
{
"Authorization": "Bearer YOUR_API_KEY"
}
Make sure there is a space between Bearer and the API key.
Common mistake
Do not use only the API key as the value of the Authorization header. The word Bearer and the space after it must be included.
The API key suddenly stopped working
Create a new key under API keys and replace the previous key in your application or automation.
Then check every place where the old key was used:
- Your own application
- Make or n8n
- HTTP Requests
- Server environment variables
- Testing tools such as Postman
- Other connected systems
Only deactivate the old key once the connection with the new key has been tested successfully.
The API key is no longer shown after it was created
A newly created API key is only shown in full once. After you close the window or leave the page, you cannot view it again.
In this case, create a new key and save it right away in a secure place.
Suitable options include:
- A password manager
- A protected environment variable
- A secret manager from your hosting provider
- The encrypted credentials area of your automation software
Do not store API keys in the open
Do not store the key directly in source code, in a public spreadsheet, in a screenshot or in a freely accessible document.
The key was copied but does not work
When copying, invisible spaces or line breaks can be carried over.
Check the value in a plain text editor. The key should be pasted without quotation marks, extra spaces or line breaks.
Wrong:
"YOUR_API_KEY "
Correct:
YOUR_API_KEY
Also check whether your application automatically adds or cuts off characters when saving.
The API key is sent in the wrong place
Depending on the endpoint, the key is sent in the Authorization header or in a dedicated field.
For the Outbound API, for example, the key can be in the body:
{
"apiKey": "YOUR_API_KEY",
"fromNumber": "+43123456789",
"toNumber": "+436641234567",
"agentId": "YOUR_AGENT_ID"
}
Alternatively, you can send it in the header:
{
"Authorization": "Bearer YOUR_API_KEY"
}
Use the variant documented for your endpoint. You can find the current endpoints and examples in our API documentation.
The request works in Postman but not in your own application
Compare both requests carefully:
- Is the same URL used?
- Is the HTTP method identical?
- Is the same API key used?
- Is the Authorization header present?
- Is
Content-Type: application/jsonbeing sent? - Is the request body valid JSON?
- Are environment variables loaded correctly?
Example:
Content-Type: application/json
Authorization: Bearer YOUR_API_KEY
If the key is loaded from an environment variable, check whether the application needs to be restarted after a change.
A request still returns 401
Create a new API key and test it first with the simplest possible request.
If the new key works, the old key was invalid, incomplete or no longer active.
If the new key does not work either, check:
- Are you using the correct API endpoint?
- Is your fonio account active?
- Is the feature you want available in your plan?
- Are method, headers and body set up correctly?
- Is the key being removed along the way by a proxy or an automation?
The API key was published by accident
Create a new key immediately and remove or deactivate the published key.
Then replace it in all connected systems.
An API key counts as exposed if it was visible in places such as:
- A public Git repository
- A screenshot or screen recording
- A freely accessible document
- A chat or email to unauthorized people
- Public logs
- Frontend code of a website
Important
A published API key should not be used any further, even if no unauthorized requests are visible so far.
If you contact us
Write to us at support@fonio.ai and include the following details:
- Email address of your fonio account
- Endpoint used
- HTTP method
- HTTP status code
- Cleaned-up request body
- Cleaned-up headers
- Date and exact time of the failed request
- Server response
Replace the API key in your examples with a placeholder:
YOUR_API_KEY
Never send us your API key
Never send us your full API key. Endpoint, status code, request and error message are enough for troubleshooting.
