To use the SDK, import the DiscordSDK class and instantiate it with your OAuth2 Client ID.
Follow these steps in your setup sequence:
- Wait for Ready: Call
await discordSdk.ready() to wait for the READY payload from the Discord client. - Authorize: Use
discordSdk.commands.authorize to trigger the OAuth permission modal. You must specify client_id, response_type, state, prompt, and the required scope array (e.g., ['identify', 'applications.commands']). - Exchange Code for Token: Send the returned
code to your application's server to retrieve an access_token. - Authenticate: Call
await discordSdk.commands.authenticate({ access_token }) to authenticate the session with the Discord client.
Note: Replace YOUR_OAUTH2_CLIENT_ID with your actual credentials found in the Discord Developer Portal.
import {DiscordSDK} from '@discord/embedded-app-sdk';
const discordSdk = new DiscordSDK(YOUR_OAUTH2_CLIENT_ID);
async function setup() {
// Wait for READY payload from the discord client
await discordSdk.ready();
// Pop open the OAuth permission modal and request for access to scopes listed in scope array below
const {code} = await discordSdk.commands.authorize({
client_id: YOUR_OAUTH2_CLIENT_ID,
response_type: 'code',
state: '',
prompt: 'none',
scope: ['identify', 'applications.commands'],
});
// Retrieve an access_token from your application's server
const response = await fetch('/.proxy/api/token', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
code,
}),
});
const {access_token} = await response.json();
// Authenticate with Discord client (using the access_token)
auth = await discordSdk.commands.authenticate({
access_token,
});
}