Shared Resources
The first step in implementing OAuth2 is registering a developer application and retrieving your client ID and client secret. Most people who will be implementing OAuth2 will want to find and utilize a library in the language of their choice. For those implementing OAuth2 from scratch, please see RFC 6749 for details. After you create your application with Discord, make sure that you have yourclient_id and client_secret handy. The next step is to figure out which OAuth2 flow is right for your purposes.
OAuth2 URLs
| URL | Description | 
|---|---|
| https://discord.com/oauth2/authorize | Base authorization URL | 
| https://discord.com/api/oauth2/token | Token URL | 
| https://discord.com/api/oauth2/token/revoke | Token Revocation URL | 
In accordance with the relevant RFCs, the token and token revocation URLs will only accept a content type of 
application/x-www-form-urlencoded. JSON content is not permitted and will return an error.OAuth2 Scopes
These are a list of all the OAuth2 scopes that Discord supports. Some scopes require approval from Discord to use. Requesting them from a user without approval from Discord may cause errors or undocumented behavior in the OAuth2 flow.| Name | Description | 
|---|---|
| activities.read | allows your app to fetch data from a user’s “Now Playing/Recently Played” list — not currently available for apps | 
| activities.write | allows your app to update a user’s activity - not currently available for apps (NOT REQUIRED FOR GAMESDK ACTIVITY MANAGER) | 
| applications.builds.read | allows your app to read build data for a user’s applications | 
| applications.builds.upload | allows your app to upload/update builds for a user’s applications - requires Discord approval | 
| applications.commands | allows your app to add commands to a guild - included by default with the botscope | 
| applications.commands.update | allows your app to update its commands using a Bearer token - client credentials grant only | 
| applications.commands.permissions.update | allows your app to update permissions for its commands in a guild a user has permissions to | 
| applications.entitlements | allows your app to read entitlements for a user’s applications | 
| applications.store.update | allows your app to read and update store data (SKUs, store listings, achievements, etc.) for a user’s applications | 
| bot | for oauth2 bots, this puts the bot in the user’s selected guild by default | 
| connections | allows /users/@me/connectionsto return linked third-party accounts | 
| dm_channels.read | allows your app to see information about the user’s DMs and group DMs - requires Discord approval | 
| enables /users/@meto return anemail | |
| gdm.join | allows your app to join users to a group dm | 
| guilds | allows /users/@me/guildsto return basic information about all of a user’s guilds | 
| guilds.join | allows /guilds/{guild.id}/members/{user.id}to be used for joining users to a guild | 
| guilds.members.read | allows /users/@me/guilds/{guild.id}/memberto return a user’s member information in a guild | 
| identify | allows /users/@mewithoutemail | 
| messages.read | for local rpc server api access, this allows you to read messages from all client channels (otherwise restricted to channels/guilds your app creates) | 
| relationships.read | allows your app to know a user’s friends and implicit relationships - requires Discord approval | 
| role_connections.write | allows your app to update a user’s connection and metadata for the app | 
| rpc | for local rpc server access, this allows you to control a user’s local Discord client - requires Discord approval | 
| rpc.activities.write | for local rpc server access, this allows you to update a user’s activity - requires Discord approval | 
| rpc.notifications.read | for local rpc server access, this allows you to receive notifications pushed out to the user - requires Discord approval | 
| rpc.voice.read | for local rpc server access, this allows you to read a user’s voice settings and listen for voice events - requires Discord approval | 
| rpc.voice.write | for local rpc server access, this allows you to update a user’s voice settings - requires Discord approval | 
| voice | allows your app to connect to voice on user’s behalf and see all the voice members - requires Discord approval | 
| webhook.incoming | this generates a webhook that is returned in the oauth token response for authorization code grants | 
In order to add a user to a guild, your bot has to already belong to that guild. 
role_connections.write cannot be used with the Implicit grant type.State and Security
Before we dive into the semantics of the different OAuth2 grants, we should stop and discuss security, specifically the use of thestate parameter. Cross-site request forgery, or CSRF, and Clickjacking are security vulnerabilities that must be addressed by individuals implementing OAuth. This is typically accomplished using the state parameter. state is sent in the authorization request and returned back in the response and should be a value that binds the user’s request to their authenticated state. For example, state could be a hash of the user’s session cookie, or some other nonce that can be linked to the user’s session.
When a user begins an authorization flow on the client, a state is generated that is unique to that user’s request. This value is stored somewhere only accessible to the client and the user, i.e. protected by the same-origin policy. When the user is redirected, the state parameter is returned. The client validates the request by checking that the state returned matches the stored value. If they match, it is a valid authorization request. If they do not match, it’s possible that someone intercepted the request or otherwise falsely authorized themselves to another user’s resources, and the request should be denied.
While Discord does not require the use of the state parameter, we support it and highly recommend that you implement it for the security of your own applications and data.
Authorization Code Grant
The authorization code grant is what most developers will recognize as “standard OAuth2” and involves retrieving an access code and exchanging it for a user’s access token. It allows the authorization server to act as an intermediary between the client and the resource owner, so the resource owner’s credentials are never shared directly with the client. All calls to the OAuth2 endpoints require either HTTP Basic authentication orclient_id and client_secret supplied in the form data body.
Authorization URL Example
client_id is your application’s client_id. scope is a list of OAuth2 scopes separated by url encoded spaces (%20). redirect_uri is whatever URL you registered when creating your application, url-encoded. state is the unique string mentioned in State and Security.
When someone navigates to this URL, they will be prompted to authorize your application for the requested scopes. On acceptance, they will be redirected to your redirect_uri, which will contain an additional querystring parameter, code. state will also be returned if previously sent, and should be validated at this point.
prompt controls how the authorization flow handles existing authorizations. If a user has previously authorized your application with the requested scopes and prompt is set to consent, it will request them to reapprove their authorization. If set to none, it will skip the authorization screen and redirect them back to your redirect URI without requesting their authorization. For passthrough scopes, like bot and webhook.incoming, authorization is always required.
The integration_type parameter specifies the installation context for the authorization. The installation context determines where the application will be installed, and is only relevant when scope contains applications.commands. When set to 0 (GUILD_INSTALL) the application will be authorized for installation to a server, and when set to 1 (USER_INSTALL) the application will be authorized for installation to a user. The application must be configured in the Developer Portal to support the provided integration_type.
Redirect URL Example
code is now exchanged for the user’s access token by making a POST request to the token URL with the following parameters:
- grant_type- must be set to- authorization_code
- code- the code from the querystring
- redirect_uri- the- redirect_uriassociated with this authorization, usually from your authorization URL
Access Token Exchange Example
Access Token Response
expires_in is how long, in seconds, until the returned access token expires, allowing you to anticipate the expiration and refresh the token. To refresh, make another POST request to the token URL with the following parameters:
- grant_type- must be set to- refresh_token
- refresh_token- the user’s refresh token
Refresh Token Exchange Example
Token Revocation Example
To disable an access or refresh token, you can revoke it by making aPOST request to the token revocation URL with the following parameters:
- token- the access token or refresh token to revoke
- token_type_hint(optional) - the- tokenparameter’s type—either- access_tokenor- refresh_token
When you revoke a token, any active access or refresh tokens associated with that authorization will be revoked, regardless of the 
token and token_type_hint values you pass in.Implicit Grant
The implicit OAuth2 grant is a simplified flow optimized for in-browser clients. Instead of issuing the client an authorization code to be exchanged for an access token, the client is directly issued an access token. The URL is formatted as follows:Authorization URL Example
access_token, token_type, expires_in, scope, and state(if specified). These are not querystring parameters. Be mindful of the ”#” character:
Redirect URL Example
Client Credentials Grant
The client credential flow is a quick and easy way for bot developers to get their own bearer tokens for testing purposes. By making aPOST request to the token URL with a grant type of client_credentials, using Basic authentication with your client id as the username and your client secret as the password, you will be returned an access token for the bot owner. Therefore, always be super-extra-very-we-are-not-kidding-like-really-be-secure-make-sure-your-info-is-not-in-your-source-code careful with your client_id and client_secret. We don’t take kindly to imposters around these parts.
You can specify scopes with the scope parameter, which is a list of OAuth2 scopes separated by spaces:
Team applications are limited to the 
identify and applications.commands.update scope, because teams are not bound to a specific user.Client Credentials Token Request Example
Client Credentials Access Token Response
Bot Users
Discord’s API provides bot users, which are a separate type of user dedicated to automation. Bot users are automatically added to all apps, and are authenticated using the bot token found in your app’s settings. Unlike the normal OAuth2 flow, bot users have full access to most API routes without using bearer tokens, and can connect to the Real Time Gateway.Bot vs User Accounts
Developers must abide by the terms of service, which includes refraining from automating standard user accounts (generally called “self-bots”) outside of the OAuth2/bot API.
- Bots are added to guilds through the OAuth2 API, and cannot accept normal invites.
- Bots cannot have friends or be added to or join Group DMs.
- Verified bots do not have a maximum number of guilds.
- Bots have an entirely separate set of rate limits.
Bot Authorization Flow
Bot authorization is a special server-less and callback-less OAuth2 flow that makes it easy for users to add bots to guilds. The URL you create looks similar to what we use for full stack implementation:Bot Auth Parameters
| name | description | 
|---|---|
| client_id | your app’s client id | 
| scope | needs to include botfor the bot flow | 
| permissions | the permissions you’re requesting | 
| guild_id | pre-fills the dropdown picker with a guild for the user | 
| disable_guild_select | trueorfalse—disallows the user from changing the guild dropdown | 
URL Example
scope parameter should be set to bot. There’s also a new parameter, permissions, which is an integer corresponding to the permission calculations for the bot. You’ll also notice the absence of response_type and redirect_uri. Bot authorization does not require these parameters because there is no need to retrieve the user’s access token.
When the user navigates to this page, they’ll be prompted to add the bot to a guild in which they have proper permissions. On acceptance, the bot will be added. Super easy!
If you happen to already know the ID of the guild the user will add your bot to, you can provide this ID in the URL as a guild_id=GUILD_ID parameter.
When the authorization page loads, that guild will be preselected in the dialog if that user has permission to add the bot to that guild. You can use this in conjunction with the parameter disable_guild_select=true to disallow the user from picking a different guild.
If your bot is super specific to your private clubhouse, or you just don’t like sharing, you can leave the Public Bot option unchecked in your application’s settings. If unchecked, only you can add the bot to guilds. If marked as public, anyone with your bot’s URL can add it to guilds in which they have proper permissions.
Advanced Bot Authorization
Devs can extend the bot authorization functionality. You can request additional scopes outside ofbot and applications.commands, which will prompt a continuation into a complete authorization code grant flow and add the ability to request the user’s access token. If you request any scopes outside of bot and applications.commands, response_type is again mandatory; we will also automatically redirect the user to the first URI in your application’s registered list unless redirect_uri is specified.
When receiving the access code on redirect, there will be additional querystring parameters of guild_id and permissions. The guild_id parameter should only be used as a hint as to the relationship between your bot and a guild. To be sure of the relationship between your bot and the guild, consider requiring the Oauth2 code grant in your bot’s settings. Enabling it requires anyone adding your bot to a server to go through a full OAuth2 authorization code grant flow. When you retrieve the user’s access token, you’ll also receive information about the guild to which your bot was added:
Extended Bot Authorization Access Token Example
Two-Factor Authentication Requirement
For bots with elevated permissions (permissions with a* next to them), we enforce two-factor authentication on the owner’s account when added to guilds that have server-wide 2FA enabled.
Webhooks
Discord’s webhook flow is a specialized version of an authorization code implementation. In this case, thescope querystring parameter needs to be set to webhook.incoming:
URL Example
redirect_uri. The URL will contain the code querystring parameter which should be exchanged for an access token. In return, you will receive a slightly modified token response:
Webhook Token Response Example
webhook.token and webhook.id. See the execute webhook documentation for how to send messages with the webhook.
Any user that wishes to add your webhook to their channel will need to go through the full OAuth2 flow. A new webhook is created each time, so you will need to save the token and id. If you wish to send a message to all your webhooks, you’ll need to iterate over each stored id:token combination and make POST requests to each one. Be mindful of our Rate Limits!
Get Current Bot Application Information
GET/oauth2/applications/@me
Returns the bot’s application object.
Get Current Authorization Information
GET/oauth2/@me
Returns info about the current authorization. Requires authentication with a bearer token.
Response Structure
| Field | Type | Description | 
|---|---|---|
| application | partial application object | the current application | 
| scopes | array of strings | the scopes the user has authorized the application for | 
| expires | ISO8601 timestamp | when the access token expires | 
| user? | user object | the user who has authorized, if the user has authorized with the identifyscope |