{"record":{"id":"65b10a57ef62e56b","repo":"discordjs/discord.js","slug":"encountered-http-401-with-error-data-code-da","errorCode":null,"errorMessage":"Encountered HTTP 401 with error ${data.code}: ${data.message}. Your token will be removed from this REST instance. If you are using @discordjs/rest directly, consider adding 'auth: false' to the request. Open an issue with your library if not.","messagePattern":"Encountered HTTP 401 with error (.+?): (.+?)\\. Your token will be removed from this REST instance\\. If you are using @discordjs/rest directly, consider adding 'auth: false' to the request\\. Open an issue with your library if not\\.","errorType":"console","errorClass":null,"httpStatus":401,"severity":"warning","filePath":"packages/rest/src/lib/handlers/Shared.ts","lineNumber":197,"sourceCode":"\n\t\t// We are out of retries, throw an error\n\t\tthrow new HTTPError(status, res.statusText, method, url, requestData);\n\t} else {\n\t\t// Handle possible malformed requests\n\t\tif (status >= 400 && status < 500) {\n\t\t\t// The request will not succeed for some reason, parse the error returned from the api\n\t\t\tconst data = (await parseResponse(res)) as DiscordErrorData | OAuthErrorData;\n\t\t\tconst isDiscordError = 'code' in data;\n\n\t\t\t// If we receive this status code, it means the token we had is no longer valid.\n\t\t\tif (status === 401 && requestData.auth === true) {\n\t\t\t\tif (isDiscordError && data.code !== 0 && !authFalseWarningEmitted) {\n\t\t\t\t\tconst errorText = `Encountered HTTP 401 with error ${data.code}: ${data.message}. Your token will be removed from this REST instance. If you are using @discordjs/rest directly, consider adding 'auth: false' to the request. Open an issue with your library if not.`;\n\t\t\t\t\t// Use emitWarning if possible, probably not available in edge / web\n\t\t\t\t\tif (typeof globalThis.process !== 'undefined' && typeof globalThis.process.emitWarning === 'function') {\n\t\t\t\t\t\tglobalThis.process.emitWarning(errorText);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconsole.warn(errorText);\n\t\t\t\t\t}\n\n\t\t\t\t\tauthFalseWarningEmitted = true;\n\t\t\t\t}\n\n\t\t\t\tmanager.setToken(null!);\n\t\t\t}\n\n\t\t\t// throw the API error\n\t\t\tthrow new DiscordAPIError(data, isDiscordError ? data.code : data.error, status, method, url, requestData);\n\t\t}\n\n\t\treturn res;\n\t}\n}\n","sourceCodeStart":179,"sourceCodeEnd":213,"githubUrl":"https://github.com/discordjs/discord.js/blob/a81ed8a306d37fdc746e26a634b6a42164ba2c8c/packages/rest/src/lib/handlers/Shared.ts#L179-L213","documentation":"This is not a thrown exception but an emitted warning (process.emitWarning or console.warn) when Discord responds HTTP 401 with a non-zero error code on an authenticated request. The REST manager then nulls its token, since the token is definitively invalid, and warns library authors that their library is forwarding an invalid token.","triggerScenarios":"Any authenticated request receiving 401 Unauthorized from Discord with a Discord error code — invalid, reset, or revoked bot token being used.","commonSituations":"Token regenerated in the Discord Developer Portal while the bot runs; DISCORD_TOKEN containing whitespace/quotes from .env parsing; accidentally using a user token or a client secret instead of a bot token; token leaked and reset by Discord.","solutions":["Regenerate and correctly configure a valid bot token, then restart the application","Check the token string for stray whitespace, quotes, or a 'Bot ' prefix mistake","Verify the token is a bot token (from the Developer Portal Bot page), not a client secret","If intentionally making unauthenticated requests, pass auth: false in the request options","After this warning the REST instance token is cleared — re-create/re-authenticate the manager to continue authenticated calls"],"exampleFix":"// before\nawait rest.get(Routes.user('@me')); // 401: invalid token\n// after\nconst token = process.env.DISCORD_TOKEN?.trim();\nif (!token) throw new Error('DISCORD_TOKEN is missing');\nconst rest = new REST().setToken(token);\nawait rest.get(Routes.user('@me'));","handlingStrategy":"try-catch","validationCode":"const token = process.env.DISCORD_TOKEN?.trim();\nif (!token || !/^Bot\\s|^[A-Za-z0-9_-]{20,}/.test(token)) {\n  throw new Error('DISCORD_TOKEN looks invalid; regenerate it in the Developer Portal');\n}","typeGuard":"function looksLikeBotToken(t: string | undefined): t is string {\n  return typeof t === 'string' && t.length >= 50 && !t.includes(' ');\n}","tryCatchPattern":"process.on('warning', (w) => {\n  if (w.message?.includes('Encountered HTTP 401')) {\n    console.error('Discord rejected the bot token; re-authenticate the REST instance');\n  }\n});","preventionTips":["Trim and validate the token read from environment/config","Use auth: false for requests that do not need authentication","Alert on HTTP 401 responses so a rotated/reset token is detected immediately","Remember the manager clears its token after this warning — plan for re-initialization"],"tags":["http-401","authentication","token","rest"],"backgroundTag":"invalid-discord-token","analyzedSha":"a81ed8a306d37fdc746e26a634b6a42164ba2c8c","analyzedAt":"2026-08-30T04:07:22.193Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}