Back

Discord API Essential Guide

Jul 31, 20246 minute read

What type of API does Discord provide?

Discord primarily uses a REST API. REST APIs are well-suited for web and mobile applications that require efficient data transfer and scalability. They use standard HTTP methods and status codes, which simplifies implementation and error handling. REST APIs are generally simpler and more lightweight than SOAP responses, and they often use JSON or XML formatting.

While Discord may use additional API types or protocols for specific features, especially for real-time functionality, their main public API for developers is most likely REST-based.

Does the Discord API have webhooks?

Yes, the official Discord API does have webhooks, but they are primarily used for sending messages to Discord channels rather than subscribing to events. Here are the key points about Discord webhooks:

Webhook Functionality

  • Discord webhooks provide a way to send automated messages and data updates into a Discord channel from external sources.

  • Webhooks in Discord are outgoing webhooks, meaning they allow external applications to send messages into Discord, rather than Discord sending events out to other systems.

Creating and Using Webhooks

  • Webhooks can be created through the Discord user interface or via the Discord API.

  • To use a webhook, you send an HTTP POST request to the webhook URL with a payload containing the message content.

  • The only required property in a webhook payload is the content property, which contains the text message to be posted to the channel.

Limitations and Considerations

  • Discord webhooks are only supported in text-based channels, not voice channels.

  • While webhooks allow sending messages into Discord, they don't provide a way to subscribe to events or receive data from Discord.

Alternative for Event Subscription

  • For subscribing to events like users joining or leaving voice channels, Discord offers a different mechanism called Gateways.

  • The Gateway API uses WebSockets and provides events like Voice State Update, which can be used to track voice channel activity.

  • To use these events, you need to create a Discord bot and enable the appropriate intents.

In summary, while Discord does have webhooks, they are not designed for event subscription. For your use case of gathering voice channel activity statistics, you would need to use the Discord Gateway API with a bot instead of webhooks.

Rate Limits and other limitations

Here are the key points about Discord API rate limits:

Global Rate Limit

  • Discord implements a global rate limit of 50 requests per second.

Route-Specific Rate Limits

  • In addition to the global limit, Discord has route-specific rate limits that need to be considered.

Rate Limit Response

  • When the rate limit is exceeded, you will receive an HTTP 429 response.
  • The response will include a retry_after value that indicates how long to wait before sending another request.

Best Practices

  • Use interaction-based commands where possible to avoid hitting rate limits.
  • Implement rate limiting at multiple layers of your application architecture.
  • Monitor your API usage closely to understand how often limits are being reached.
  • Consider offering a free tier with adequate limits for users to explore your API.

Handling Rate Limits

  • When a rate limit is exceeded, you may choose to throttle requests rather than completely stopping them.
  • Set up proper error handling to deal with 429 responses gracefully.

Additional Considerations

  • Rate limits can be implemented based on users, locations, or servers.
  • Understanding burst limits (throttling over very short time intervals) is important.

The specific rate limits can vary by endpoint and may change over time, so it's always best to refer to the official Discord API documentation for the most up-to-date information on rate limits for specific endpoints.

Latest API Version

The most recent version of the Discord API is v10. Here are the key points:

  1. Discord API v10 is the current stable version.

  2. The Discord API Types package, which provides TypeScript definitions for the Discord API, is currently at version 0.37.93.

  3. To use the latest API version in your code, you would typically import it like this:

    import { APIUser } from 'discord-api-types/v10';
  4. The Discord API is regularly updated, but major version changes (like v9 to v10) are less frequent.

  5. It's important to keep your Discord bot or application up to date with the latest API version to ensure compatibility and access to new features.

  6. Discord provides detailed documentation for each API version, which you can find on their developer portal.

When working with the Discord API, it's best practice to:

  • Always specify the API version you're targeting in your imports or API calls.
  • Keep an eye on the Discord Developer Portal for announcements about new API versions or changes.
  • Regularly update your dependencies, especially discord-api-types, to ensure you have access to the latest types and features.

Remember that while v10 is the current stable version, Discord may introduce new features or changes in minor updates within this version.

How to get a Discord developer account and API Keys?

To get a developer account for Discord and create an API integration, follow these steps:

  1. Create a Discord account: If you don't already have a Discord account, you'll need to create one at https://discord.com.

  2. Access the Discord Developer Portal: Go to the Discord Developer Portal at https://discord.com/developers/applications.

  3. Create a new application:

    • Click on the "New Application" button.
    • Give your application a name and agree to Discord's Developer Terms of Service and Developer Policy.
    • Click "Create" to finalize the creation of your application.
  4. Set up your application:

    • In the application settings, you can customize various aspects of your app, such as its description, icon, and other details.
    • Navigate to the "Bot" section in the left sidebar to add a bot to your application if needed.
  5. Get your API credentials:

    • In the "General Information" tab, you'll find your Client ID and Client Secret. These are essential for API authentication.
    • If you've added a bot, you can find the bot token in the "Bot" section. Keep this token secure, as it provides access to your bot.
  6. Set up OAuth2:

    • Go to the "OAuth2" section in the left sidebar.
    • Select the scopes and permissions your application needs.
    • Use the generated OAuth2 URL to add your bot to servers or authorize your application.

What can you do with the Discord API?

Based on the Discord API documentation provided, here's a list of data models you can interact with using the Discord API, along with what is possible for each:

Interaction

  • Represents user actions that need to be notified (e.g., slash commands and components)
  • Attributes include:
    • app_permissions: Application permissions
    • channel: The channel where the interaction occurred
    • guild: The guild where the interaction occurred (if applicable)
    • user: The user who initiated the interaction
    • token: Token to continue the interaction (valid for 15 minutes)
    • data: Interaction data
  • Methods include:
    • delete_original_response(): Delete the original interaction response
    • edit_original_response(): Edit the original interaction response
    • original_response(): Fetch the original interaction response message

InteractionResponse

  • Handles responding to interactions
  • Methods include:
    • send_message(): Send a message in response to the interaction
    • defer(): Acknowledge the interaction and indicate a response will come later
    • edit_message(): Edit the message that triggered the interaction
    • send_modal(): Send a modal in response to the interaction

InteractionMessage

  • Represents a message sent as a response to an interaction

Component

  • Base class for UI components (e.g., buttons, select menus)

Button

  • Represents a clickable button in Discord's UI
  • Can be customized with label, style, and emoji

SelectMenu

  • Represents a dropdown menu in Discord's UI
  • Can have multiple options for users to choose from

TextInput

  • Represents a text input field in a modal

AppCommand

  • Represents an application command (slash command)

View

  • Represents a container for multiple UI components
  • Represents a modal dialog with input fields

CommandTree

  • Manages the registration and handling of application commands

Translator

  • Handles translations for internationalization

Enumerations

  • Various enumerations for interaction types, component types, etc.

For each of these data models, you can typically:

  • Create instances (where applicable)
  • Access and modify attributes
  • Call associated methods
  • Use them in responses to interactions
  • Handle events related to them (e.g., button clicks, command invocations)

The exact capabilities depend on the specific data model and the context in which it's used within the Discord API.