Skip to main content

PyPI - Version PyPI - Python Version PyPI - Types PyPI - License GitHub Actions Workflow Status


Pycord REST enables you to build Discord applications that respond to:

  • Interactions via HTTP endpoints (slash commands, components, modals)
  • Webhook events such as application authorization and entitlements

Built on:

  • FastAPI - For handling HTTP requests
  • py-cord - For Discord command builders and interaction handling
  • uvicorn - ASGI server implementation

Quick Start

from pycord_rest import App
import discord

app = App()

@app.slash_command(name="ping", description="Responds with pong!")
async def ping(ctx):
    await ctx.respond("Pong!")

if __name__ == "__main__":
    app.run(
        token="YOUR_BOT_TOKEN",
        public_key="YOUR_PUBLIC_KEY",  # From Discord Developer Portal
        uvicorn_options={
            "host": "0.0.0.0",
            "port": 8000
        }
    )

Core Concepts

How It Works

Pycord REST creates an HTTP server that:

  1. Listens for Discord interaction requests and webhook events
  2. Verifies request signatures using your application's public key
  3. Routes events to appropriate handlers
  4. Returns responses back to Discord

Unlike traditional WebSocket-based Discord bots, HTTP-based applications:

  • Only wake up when receiving interactions or webhook events
  • Don't maintain a persistent connection to Discord's gateway
  • Don't receive most real-time Discord events

Discord Application Setup

  1. Create an application on the Discord Developer Portal
  2. Copy your public key to verify signatures
  3. Run the Pycord REST app
  4. Configure the endpoints:
  • Interactions Endpoint URL - For slash commands and component interactions (https://example.com)
  • Webhook URL - For receiving application events (e.g., https://example.com/webhook)

IMPORTANT: Don't forget to run your FastAPI server before setting up the application on Discord, or else Discord won't be able to verify the endpoints.

Features

Interaction Handling

Respond to Discord interactions such as:

  • Slash Commands - Create and respond to application commands
  • UI Components - Buttons, select menus, and other interactive elements
  • Modal Forms - Pop-up forms for gathering user input
  • Autocomplete - Dynamic option suggestions as users type

Webhook Events

Handle Discord webhook events such as:

  • Application authorization - When your app is added to a guild or authorized by a user
  • Entitlement creation - When a user subscribes to your app's premium features

Type Safety

Pycord REST is fully type-annotated and type-safe. It uses basedpyright for type checking.

NOTE: While Pycord REST itself is fully typed, the underlying py-cord library has limited type annotations, which may affect type checking in some areas.

Usage Examples

TIP: For complete examples, check out the examples directory.

Basic Commands

Commands use the familiar py-cord syntax:

@app.slash_command(name="hello", description="Say hello")
async def hello(ctx, user: discord.Member = None):
    user = user or ctx.author
    await ctx.respond(f"Hello {user.mention}!")

@app.slash_command()
async def button(ctx):
    view = discord.ui.View()
    view.add_item(discord.ui.Button(label="Click me!", custom_id="my_button"))
    await ctx.respond("Press the button!", view=view)

Event Handling

The possible events are:

  • on_application_authorized - When your app is added to a guild or authorized by a user
  • on_entitlement_create - When a user subscribes to your app's premium features

NOTE: For application installation events, use on_application_authorized instead of on_guild_join.

@app.listen("on_application_authorized")
async def on_application_authorized(event: ApplicationAuthorizedEvent):
    # Triggers when app is added to a guild OR when a user authorizes your app
    print(f"Authorization received: Guild={event.guild}, User={event.user}")

Custom Routes

Add your own FastAPI routes:

from fastapi import Request

@app.router.get("/custom")
async def custom_endpoint(request: Request):
    return {"message": "This is a custom endpoint"}

Configuration

app.run(
    token="YOUR_BOT_TOKEN",
    public_key="YOUR_PUBLIC_KEY",
    uvicorn_options={
        "host": "0.0.0.0",  # Listen on all network interfaces
        "port": 8000,        # Port to listen on
        "log_level": "info", # Uvicorn logging level
    },
    health=True  # Enable /health endpoint for monitoring
)

Integration Options

  1. Stand-alone HTTP Interaction Bot - Commands and components only
  2. Webhook Event Handler Only - Process application events alongside a separate gateway bot
  3. Full HTTP Application - Handle both interactions and webhook events

Limitations

Since Pycord REST doesn't use Discord's WebSocket gateway:

  • No Cache - No local storage of guilds, channels, or users
  • Limited API Methods - Functions that rely on cache won't work:
    • app.get_channel(), app.get_guild(), app.get_user()
    • Presence updates
    • Voice support
    • Member tracking
  • Limited Events - Only interaction-based and webhook events work

Getting Help

If you encounter issues or have questions about pycord-rest:

TIP: Before asking for help, check if your question is already answered in the examples directory or existing GitHub issues.

Development

Local Testing

Use tunneling tools to expose your local development server:

  • ngrok:

    # Install ngrok
    npm install -g ngrok
    
    # Expose your local server
    ngrok http 8000
    
  • Cloudflare Tunnel or localtunnel - Alternative tunneling options

These tools provide temporary URLs for testing without deploying to production.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run linter, formatter and type checker: ruff check .,ruff format ., basedpyright .
  5. Submit a pull request

Development Tools:

  • uv: For dependency management
  • Ruff: For linting and formatting
  • HashiCorp Copywrite: For managing license headers
  • basedpyright: For type checking

NOTE: This is an early-stage project and may have unexpected behaviors or bugs. Please report any issues you encounter.

License

MIT License - Copyright (c) 2025 Paillat-dev


Made with ❤ by Paillat-dev

Metadata

Release files for pycord-rest-bot 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycord-rest-bot 0.3.1
File Size Uploaded Provenance Actions
pycord_rest_bot-0.3.1.tar.gz 11.0 kB Without provenance Details for pycord_rest_bot-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pycord-rest-bot 0.3.1
File Interpreter ABI Platform Actions
pycord_rest_bot-0.3.1-py3-none-any.whl Python 3 none any Details for pycord_rest_bot-0.3.1-py3-none-any.whl

Total release size: 21.7 kB

Release files / pycord_rest_bot-0.3.1.tar.gz

Download URL pycord_rest_bot-0.3.1.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
98ea7a90995644a7d3f9f357d7ec4781c4add2a1671b16569075667c072f1a4c
BLAKE2b-256 checksum
How to use checksums
7912050778364461a70f17e6323a0c8c87fb0ab94c7e0940d002d05022404c21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pycord_rest_bot-0.3.1-py3-none-any.whl

Download URL pycord_rest_bot-0.3.1-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
389389d24beca07d572c5db7d0384e12890f560d0eff6ca67f475e41976c1e9a
BLAKE2b-256 checksum
How to use checksums
8b334d5c95ac2b9d8e76dfbfaec88c3ed15d25dfd5304305e123cff6aae234fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for pycord-rest-bot 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page