Discord error 40001: Unauthorized
Last verified against Discord Developer Docs - Opcodes and status codes
Unauthorized. Provide a valid token and try againWhat error 40001 means
The request carried no usable credentials. Either the Authorization header was missing, was empty, or used a scheme Discord did not recognize for that route. This is different from 50014 Invalid authentication token, which means a token was present and parsed but rejected. 40001 is "I did not get a token at all", and it is the code you see when an environment variable is undefined and your code sends Authorization: Bot undefined.
Bot tokens must be sent as Authorization: Bot <token>; OAuth2 access tokens as Authorization: Bearer <token>. Using the wrong prefix, or none, on a route that requires auth returns 40001. A handful of routes are unauthenticated (executing a webhook by URL, gateway bootstrap), so a correct token is not always needed, but when it is needed it must be well-formed.
Triage among the three token errors takes one minute. If REST returns 40001, the header is missing or malformed: no token arrived. If REST returns 50014, a token arrived and was rejected: it is wrong or revoked. If the gateway closes with 4004, the same bad-token condition hit the WebSocket. Seeing 40001 on some routes and success on others usually means the header is being dropped in one code path only, classically a fetch wrapper that whitelists headers, a proxy that strips Authorization, or a serverless function whose secret is mounted in one environment and not another. The literal string Bot undefined in a request log is the smoking gun for an unset environment variable.
What it looks like
{
"message": "401: Unauthorized",
"code": 0
}
// or, on many routes:
{
"message": "Unauthorized. Provide a valid token and try again",
"code": 40001
}Why it happens
- DISCORD_TOKEN (or equivalent) not set in the environment where the bot runs; header becomes Bot undefined or Bot
- Missing Bot / Bearer prefix, or a typo such as bot (lowercase) on some HTTP clients that are strict
- Authorization header stripped by a proxy or by a fetch wrapper that whitelists headers
- Using the client secret or public key instead of the bot token
- Sending an OAuth2 Bearer token to an endpoint that only accepts Bot auth
How to fix Discord error 40001
- 1Log the header value length (never the token itself) to confirm it is non-empty
- 2Confirm the format: Authorization: Bot
for bot routes, Authorization: Bearer for OAuth2 routes - 3Verify the deployment environment actually has the secret (container env, CI secret, .env loaded before the client is constructed)
- 4Copy the token from the Developer Portal Bot page (Reset Token if it was never saved) and redeploy
- 5Test with curl -H "Authorization: Bot $TOKEN" https://discord.com/api/v10/users/@me
How to stop it recurring
Fail fast at startup: assert the token is present and matches the expected shape before constructing the client. Add a readiness check that calls GET /users/@me once and refuses to boot on 401. Keep secrets in a manager, not in shell history or copied .env files.
If the bot also shows offline, work through the token checklist in Discord bot not responding; repeated 401s count toward the invalid-request ceiling described on the Discord limits page, so do not leave a misconfigured bot retrying overnight.
Official reference: Discord Developer Docs - Opcodes and status codes. See all Discord error codes or the Discord limits and quotas.
Related codes
- 50014: Invalid authentication token providedInvalid authentication token provided
- 4004: Authentication failedThe account token sent with your identify payload is incorrect.
- 50025: Invalid OAuth2 access token providedInvalid OAuth2 access token provided
- 20012: Not authorized to perform this action on this applicationYou are not authorized to perform this action on this application
Error 40001 - quick answers
What does Discord error 40001 mean?
The request carried no usable credentials. Either the Authorization header was missing, was empty, or used a scheme Discord did not recognize for that route. This is different from 50014 Invalid authentication token , which means a token was present and parsed but rejected.
How do I fix Discord error 40001?
1. Log the header value length (never the token itself) to confirm it is non-empty 2. Confirm the format: Authorization: Bot for bot routes, Authorization: Bearer for OAuth2 routes 3. Verify the deployment environment actually has the secret (container env, CI secret, .env loaded before the client is constructed) 4. Copy the token from the Developer Portal Bot page (Reset Token if it was never saved) and redeploy 5. Test with curl -H "Authorization: Bot $TOKEN"…
Stop debugging Discord by hand
Connect the channel through Conferbot: tokens, webhooks and retries are handled, failures show as readable status.