OAuth2
OAuth2 lets a user give an application access to their account, and the same consent step can install the application’s bot into a guild or group direct message. You create applications and client secrets with the Applications routes.
Authorisation code grant
Заголовок раздела «Authorisation code grant»- Send the user’s browser to Authorise application. When the user approves on the consent screen, Squad sends the browser back to your
redirect_uriwith acodeand yourstate. - Exchange the code at Exchange OAuth2 token with your client credentials to get an access token and a refresh token. Every third-party application gets a refresh token.
- Send the access token as
Authorization: Bearer <token>. Before the access token expires, exchange the refresh token for a new pair.
| Credential | Lifetime |
|---|---|
| Authorisation code | 10 minutes, single use |
| Access token | 7 days |
| Refresh token | 30 days, single use. Each refresh returns a new one that lasts another 30 days |
A code works for one token exchange. Once your application is authenticated, its first exchange of a code uses the code up, even if the redirect_uri or code_verifier is wrong, so after such a failure you need a new code. A code sent by another application or with a wrong client secret isn’t used up.
If you don’t refresh within 30 days, the refresh token expires and the user has to authorise your application again. Once the user has no refresh token left for your application, the application drops out of their authorised applications and prompt=none returns consent_required.
PKCE supports only the S256 method. Send code_challenge together with code_challenge_method=S256. Squad rejects plain with a validation error. At Grant OAuth2 consent, a challenge without a method or a method without a challenge gets invalid_request. With prompt=none, the same mistakes redirect with error=consent_required. When the code has a challenge, the token exchange needs the matching code_verifier.
The token, introspection and revocation routes take an application/x-www-form-urlencoded or multipart/form-data body. Authenticate your application with HTTP Basic or with the client_id and client_secret fields. When credentials are in both the Authorization header and the body, introspection and revocation return invalid_request as the error.
Grant OAuth2 consent and the routes under /oauth2/@me/authorizations need the user’s own token. A bot token or OAuth2 access token gets 403 ACCESS_DENIED.
String fields such as state, redirect_uri and code can be at most 256 characters.
OAuth2 scopes
Заголовок раздела «OAuth2 scopes»| Value | Description |
|---|---|
| identify | Read the user’s account through Get OAuth2 user information and Get current user |
| email1 | Adds email and verified to the user information |
| guilds | Use List current user guilds, Get guild and List guild roles |
| connections | Use List connections |
| bot2 | Install the application’s bot into a guild or group direct message |
1 Only works together with identify
2 The access token gets no access from this scope
Separate scopes with spaces. Squad rejects an unknown scope with invalid_scope, and the order of scopes in a response isn’t fixed.
OAuth2 error response object
Заголовок раздела «OAuth2 error response object»The token, consent, introspection and revocation routes return this body with status 400 when the grant, client, scope, redirect URI or permission mask is rejected. Other failures use the ordinary error response.
Structure
Заголовок раздела «Structure»| Field | Type | Description |
|---|---|---|
| error | string | The OAuth2 error code |
| error_description | string | A description for people, which you shouldn’t parse |
Example
Заголовок раздела «Example»{ "error": "invalid_grant", "error_description": "The provided authorization grant is invalid, expired, or revoked"}OAuth2 error codes
Заголовок раздела «OAuth2 error codes»| Value | Description |
|---|---|
| invalid_request | The redirect URI, response type, PKCE challenge or permission mask is wrong, or credentials were sent in both places |
| invalid_client | The application is unknown or the client secret is missing or wrong |
| invalid_grant | The code, refresh token, redirect URI or PKCE verifier doesn’t match a valid grant |
| invalid_scope | A requested scope isn’t in OAuth2 scopes |
OAuth2 token object
Заголовок раздела «OAuth2 token object»The token pair a successful exchange returns.
Structure
Заголовок раздела «Structure»| Field | Type | Description |
|---|---|---|
| access_token | string | The bearer token |
| token_type | string | Always Bearer |
| expires_in | integer | The access token lifetime in seconds, 604800 |
| refresh_token | string | The refresh token for the next exchange. Every third-party application gets one |
| scope | string | The granted scopes, separated by spaces |
Example
Заголовок раздела «Example»{ "access_token": "M2xQb1RkY0hqTmZLc1B2WndBcUx1RWc2WXRJbk9iUmE", "token_type": "Bearer", "expires_in": 604800, "refresh_token": "VGhpc0lzQUZha2VSZWZyZXNoVG9rZW5Gb3JEb2NzMDE", "scope": "identify email"}OAuth2 user information object
Заголовок раздела «OAuth2 user information object»The user that Get OAuth2 user information returns.
Structure
Заголовок раздела «Structure»| Field | Type | Description |
|---|---|---|
| sub | snowflake | The ID of the user, the same as id |
| id | snowflake | The ID of the user |
| username | string | The username |
| discriminator | string | The four-digit discriminator |
| global_name | ?string | The display name |
| avatar | ?string | The avatar hash |
| email? | ?string | The email address, null without the email scope |
| verified? | ?boolean | Whether the email address is verified, sent only with the email scope |
| flags? | integer | The public user flags |
| avatar_color | ?integer | The default avatar colour |
| bot | boolean | Whether the account is a bot |
| system | boolean | Whether the account is a system user |
OAuth2 introspection object
Заголовок раздела «OAuth2 introspection object»Whether a token is active, and what it grants.
Structure
Заголовок раздела «Structure»| Field | Type | Description |
|---|---|---|
| active | boolean | Whether the token is valid and was issued to your application |
| scope? | string | The granted scopes, separated by spaces |
| client_id? | snowflake | The ID of the application |
| token_type? | string | The token type, Bearer for an access token or refresh_token for a refresh token |
| exp? | integer | When the access token expires, in Unix seconds |
| iat? | integer | When the token was issued, in Unix seconds |
| sub? | snowflake | The ID of the user |
An inactive token returns only active: false. An active token has every other field, and a refresh token has no exp.
Current OAuth2 authorisation object
Заголовок раздела «Current OAuth2 authorisation object»The application, scopes and expiry of the access token you sent.
Structure
Заголовок раздела «Structure»| Field | Type | Description |
|---|---|---|
| application | OAuth2 application summary object | The application the token was issued to |
| scopes | array[string] | The granted scopes |
| expires | ISO8601 timestamp | When the access token expires |
| user? | OAuth2 authorised user object | The user, sent only when the token has identify |
OAuth2 application summary object
Заголовок раздела «OAuth2 application summary object»The application in a current OAuth2 authorisation.
Structure
Заголовок раздела «Structure»| Field | Type | Description |
|---|---|---|
| id | snowflake | The ID of the application |
| name | string | The name of the application |
| icon | ?string | The avatar hash of the application’s bot |
| description | ?string | The bio of the application’s bot |
| bot_public | boolean | Whether anyone can install the bot |
| bot_require_code_grant | boolean | Whether installing the bot needs a full code grant |
OAuth2 authorised user object
Заголовок раздела «OAuth2 authorised user object»The user in a current OAuth2 authorisation.
Structure
Заголовок раздела «Structure»| Field | Type | Description |
|---|---|---|
| id | snowflake | The ID of the user |
| username | string | The username |
| discriminator | string | The four-digit discriminator |
| global_name | ?string | The display name |
| avatar | ?string | The avatar hash |
| avatar_color | ?integer | The default avatar colour |
| bot | boolean | Whether the account is a bot |
| system | boolean | Whether the account is a system user |
| flags | integer | The public user flags |
| email? | ?string | The email address, null without the email scope |
| verified? | ?boolean | Whether the email address is verified, sent only with the email scope |
| sub | snowflake | The ID of the user, the same as id |
OAuth2 authorisation object
Заголовок раздела «OAuth2 authorisation object»One application the current user has authorised.
Structure
Заголовок раздела «Structure»| Field | Type | Description |
|---|---|---|
| application | authorised application object | The application |
| scopes1 | array[string] | The scopes the user granted to it |
| authorized_at | ISO8601 timestamp | When the oldest current refresh token for the application was issued |
1 Can include bot or values you don’t recognise, which you should ignore
Authorised application object
Заголовок раздела «Authorised application object»The application in an OAuth2 authorisation.
Structure
Заголовок раздела «Structure»| Field | Type | Description |
|---|---|---|
| id | snowflake | The ID of the application |
| name | string | The name of the application |
| icon | ?string | The avatar hash of the application’s bot |
| description | ?string | The bio of the application’s bot |
| bot_public | boolean | Whether anyone can install the bot |
OAuth2 consent response object
Заголовок раздела «OAuth2 consent response object»The callback URL from Grant OAuth2 consent.
Structure
Заголовок раздела «Structure»| Field | Type | Description |
|---|---|---|
| redirect_to | string | The redirect URI with code and your state added |
Authorise application
Заголовок раздела «Authorise application»/v1/oauth2/authorizeStarts the grant and redirects the browser to the Squad consent screen with the same parameters.
Query parameters
Заголовок раздела «Query parameters»| Field | Type | Description |
|---|---|---|
| response_type? | string | The response type, code |
| client_id | snowflake | The ID of the application |
| redirect_uri? | string | A redirect URI registered on the application |
| scope | string | The requested scopes, separated by spaces |
| state? | string | A value Squad returns unchanged on the redirect |
| prompt? | string | The prompt mode, consent or none |
| guild_id? | snowflake | The guild to preselect for the bot scope |
| channel_id? | snowflake | The group direct message to preselect for the bot scope |
| permissions? | string | The permissions to give the bot, as an integer string |
| disable_guild_select? | string | true to stop the user from picking another guild |
| code_challenge? | string | The PKCE challenge |
| code_challenge_method? | string | The PKCE method, S256 |
Response
Заголовок раздела «Response»| Status | Body | Condition |
|---|---|---|
| 302 | empty | The Location header has the consent screen or your callback |
With prompt=none, Squad skips the consent screen only when the request comes with a signed-in user session and that user has already authorised your application for every requested scope. Squad then redirects to your redirect_uri with a code. Otherwise it redirects with an error:
login_requiredwhen there is no signed-in user session.consent_requiredwhen the user hasn’t authorised every requested scope, or when the request includes thebotscope, which always needs the consent screen.consent_requiredalso when the request has a problem the consent screen would reject, such as a missing or unregisteredredirect_urior a PKCE challenge without a method.
If redirect_uri isn’t registered on the application, the error goes to the Squad web app.
The rate limit is 60 requests per minute, on the oauth:authorize bucket.
Grant OAuth2 consent
Заголовок раздела «Grant OAuth2 consent»/v1/oauth2/authorize/consentRecords the user’s consent and returns an OAuth2 consent response with the callback URL. With the bot scope it also installs the bot. In a guild you need MANAGE_GUILD or ADMINISTRATOR, and in a group direct message you need to be a recipient.
JSON body
Заголовок раздела «JSON body»| Field | Type | Description |
|---|---|---|
| response_type? | string | The response type, code. Ignored when the only scope is bot |
| client_id | snowflake | The ID of the application |
| redirect_uri?1 | string | A redirect URI registered on the application |
| scope | string | The requested scopes, separated by spaces |
| state? | string | A value Squad adds to the callback URL |
| permissions?2 | string | The permissions to give the bot, as an integer string |
| guild_id?3 | snowflake | The guild to install the bot into |
| channel_id?3 | snowflake | The group direct message to install the bot into |
| code_challenge? | string | The PKCE challenge |
| code_challenge_method? | string | The PKCE method, S256 |
1 Required unless the only scope is bot and the application doesn’t set bot_require_code_grant
2 You can only request permissions you have. Thread permissions need the channel_threads client capability
3 Send one or the other
Response
Заголовок раздела «Response»| Status | Body | Condition |
|---|---|---|
| 200 | OAuth2 consent response object | Consent was recorded |
| 400 | OAuth2 error object | The application, scope, redirect URI, response type, PKCE challenge or permission mask was rejected |
| 400 | error response | NOT_A_BOT_APPLICATION, BOT_ALREADY_IN_GUILD or INVALID_CHANNEL_TYPE |
| 400 | error response | MAX_GUILD_MEMBERS, MAX_GUILD_ROLES or MAX_GROUP_DM_RECIPIENTS |
| 403 | error response | BOT_IS_PRIVATE when you don’t own a private bot’s application |
| 403 | error response | MISSING_PERMISSIONS when you lack the guild permissions, or MISSING_ACCESS when you aren’t in the group direct message |
| 404 | error response | UNKNOWN_CHANNEL when the group direct message doesn’t exist |
When a guild install has a non-zero permissions value, Squad creates a role with the application’s name with those permissions and gives it to the bot. A guild install sends Guild Create and Guild Member Add, and a group direct message install sends Channel Recipient Add. If the install fails, Squad deletes the code and returns the error.
The rate limit is 60 requests per minute, on the oauth:authorize bucket.
Exchange OAuth2 token
Заголовок раздела «Exchange OAuth2 token»/v1/oauth2/tokenExchanges an authorisation code or a refresh token for an OAuth2 token object.
Form body
Заголовок раздела «Form body»| Field | Type | Description |
|---|---|---|
| grant_type | string | The grant type, authorization_code or refresh_token |
| code | string | The code from the callback, for authorization_code |
| redirect_uri | string | The redirect URI the code was issued for. Squad compares normalised URLs, so https://example.com and https://example.com/ match |
| code_verifier? | string | The PKCE verifier, when the code has a challenge |
| refresh_token | string | The refresh token, for refresh_token |
| client_id? | snowflake | The ID of the application, if you don’t use HTTP Basic |
| client_secret? | string | The client secret, if you don’t use HTTP Basic |
Response
Заголовок раздела «Response»| Status | Body | Condition |
|---|---|---|
| 200 | OAuth2 token object | A token pair was issued |
| 400 | OAuth2 error object | Client authentication or the grant failed |
The rate limit is 120 requests per minute, on the oauth:token bucket.
Get OAuth2 user information
Заголовок раздела «Get OAuth2 user information»/v1/oauth2/userinfoidentify. Returns the OAuth2 user information for the access token’s user.
Response
Заголовок раздела «Response»| Status | Body | Condition |
|---|---|---|
| 200 | OAuth2 user information object | The user was returned |
| 403 | error response | MISSING_OAUTH_SCOPE when the token lacks identify, with the scope in required_scope |
The rate limit is 120 requests per minute, on the oauth:introspect bucket.
Introspect OAuth2 token
Заголовок раздела «Introspect OAuth2 token»/v1/oauth2/introspectReturns an OAuth2 introspection object for an access or refresh token. A token is inactive when it was issued to another application or when its user can’t sign in.
Form body
Заголовок раздела «Form body»| Field | Type | Description |
|---|---|---|
| token | string | The access or refresh token |
| client_id? | snowflake | The ID of the application, if you don’t use HTTP Basic |
| client_secret? | string | The client secret, if you don’t use HTTP Basic |
Response
Заголовок раздела «Response»| Status | Body | Condition |
|---|---|---|
| 200 | OAuth2 introspection object | The token was checked, including an inactive one |
| 400 | OAuth2 error object | Client authentication failed |
The rate limit is 120 requests per minute, on the oauth:introspect bucket.
Revoke OAuth2 token
Заголовок раздела «Revoke OAuth2 token»/v1/oauth2/token/revokeDeletes every access and refresh token the token’s user has for your application.
Form body
Заголовок раздела «Form body»| Field | Type | Description |
|---|---|---|
| token | string | The access or refresh token |
| token_type_hint?1 | string | The token type, access_token or refresh_token |
| client_id? | snowflake | The ID of the application, if you don’t use HTTP Basic |
| client_secret? | string | The client secret, if you don’t use HTTP Basic |
1 Send refresh_token when the token is a refresh token, or Squad won’t find it
Response
Заголовок раздела «Response»| Status | Body | Condition |
|---|---|---|
| 200 | empty | The tokens were deleted, or the token matched nothing |
| 400 | OAuth2 error object | Client authentication failed |
The rate limit is 120 requests per minute, on the oauth:revoke bucket.
Get current OAuth2 authorisation
Заголовок раздела «Get current OAuth2 authorisation»/v1/oauth2/@meReturns the current OAuth2 authorisation for the OAuth2 access token you send, and needs no scope.
Response
Заголовок раздела «Response»| Status | Body | Condition |
|---|---|---|
| 200 | current OAuth2 authorisation object | The authorisation was returned |
| 401 | error response | UNAUTHORIZED when the token isn’t an OAuth2 access token |
The rate limit is 120 requests per minute, on the oauth:introspect bucket.
List OAuth2 authorisations
Заголовок раздела «List OAuth2 authorisations»/v1/oauth2/@me/authorizationsReturns an array of OAuth2 authorisation objects for the applications the current user has authorised with a scope other than bot.
Response
Заголовок раздела «Response»| Status | Body | Condition |
|---|---|---|
| 200 | array[OAuth2 authorisation object] | The authorisations were returned |
The rate limit is 60 requests per minute, on the oauth_dev:clients:list bucket.
Revoke OAuth2 authorisation
Заголовок раздела «Revoke OAuth2 authorisation»/v1/oauth2/@me/authorizations/{application_id}Deletes every token the current user has for one application. A bot the application installed stays where it is.
Response
Заголовок раздела «Response»| Status | Body | Condition |
|---|---|---|
| 204 | empty | The authorisation was revoked |
| 404 | error response | UNKNOWN_APPLICATION when the application doesn’t exist |
The rate limit is 120 requests per minute, on the oauth:introspect bucket.
Bulk revoke OAuth2 authorisations
Заголовок раздела «Bulk revoke OAuth2 authorisations»/v1/oauth2/@me/authorizations/revokeDeletes every token the current user has for several applications. If any application is unknown, the request returns 404 UNKNOWN_APPLICATION and revokes nothing.
JSON body
Заголовок раздела «JSON body»| Field | Type | Description |
|---|---|---|
| application_ids | array[snowflake] | The IDs of the applications (1-100) |
Response
Заголовок раздела «Response»| Status | Body | Condition |
|---|---|---|
| 204 | empty | The authorisations were revoked |
The rate limit is 120 requests per minute, on the oauth:introspect bucket.