Skip to main content

WebSocket-based request forwarding for FastAPI applications - develop and debug webhooks locally

Project description

fastapi-inroute

WebSocket-based request forwarding for FastAPI applications

Develop and debug webhook handlers locally by forwarding requests from test/staging deployments to your development machine. Run the same code in deployments (server mode) and locally (client mode).

Why?

When building webhook-based applications (GitHub webhooks, Stripe payments, Twilio SMS, etc.), you need a public URL for testing. FastAPI InRoute eliminates the need for ngrok or other tunneling services by:

  1. Deploying your app in server mode - receives webhooks at a public URL
  2. Running the same code locally in client mode - processes webhooks with full debugging capabilities
  3. Forwarding requests via WebSocket - server sends requests to your local client, client processes and returns responses

Quick Start

Installation

pip install fastapi-inroute

Your Application

# app.py - Same code for production AND local development
from fastapi import FastAPI, Request
from fastapi_inroute import setup_inroute

app = FastAPI()

@app.post("/webhook/github")
async def github_webhook(request: Request):
    payload = await request.json()
    print(f"Processing webhook: {payload}")
    return {"status": "processed"}

# Setup InRoute (mode determined by environment variables)
setup_inroute(app)

Test/Staging Deployment

# Set environment variables in your hosting platform
export FASTAPI_INROUTE_IS_SERVER=true
export FASTAPI_INROUTE_IS_CLIENT=false

# Deploy and run
uvicorn app:app --host 0.0.0.0 --port 8000

Configure your webhook provider (GitHub, Stripe, etc.) to send webhooks to your test/staging deployment:

https://your-test-app.com/webhook/github

Local Development

# Set environment variables locally
export FASTAPI_INROUTE_IS_SERVER=false
export FASTAPI_INROUTE_IS_CLIENT=true
export FASTAPI_INROUTE_SERVER_URL=wss://your-test-app.com/inroute

# Run the SAME code locally
uvicorn app:app --reload

Now when webhooks arrive at your test deployment URL, they'll be forwarded to your local machine for processing! Set breakpoints, add logging, modify code - all while handling real webhook payloads in a safe development environment.

How It Works

┌─────────────────────────────────────────────────────────┐
│          Test/Staging Deployment (Server Mode)           │
│                                                          │
│  GitHub/Stripe ──▶ FastAPI App ──▶ WebSocket           │
│  Webhook              (captures)      (forwards)         │
│                                          │               │
└──────────────────────────────────────────┼───────────────┘
                                           │
                                           ▼
┌──────────────────────────────────────────┼───────────────┐
│                                          │               │
│           Local Development (Client Mode)                │
│                                                          │
│              WebSocket ──▶ FastAPI App ──▶ Process      │
│              (receives)    (same code)     (debug!)      │
│                                                          │
└──────────────────────────────────────────────────────────┘

Features

  • Same codebase for test deployments and local development
  • No tunneling services required (no ngrok, localtunnel, etc.)
  • Full debugging capabilities on local machine
  • Real webhook payloads from third-party services
  • WebSocket-based forwarding with automatic reconnection
  • Simple setup with environment variables
  • Built for FastAPI - seamless integration

Use Cases

  • Webhook Development: GitHub, Stripe, Twilio, SendGrid, etc.
  • OAuth Callbacks: Test OAuth flows locally
  • Payment Processing: Debug payment webhooks (Stripe, PayPal)
  • IoT Applications: Device-to-server communication
  • Third-Party Integrations: Any service that requires a public URL

Configuration

Environment Variables

Variable Description Server Mode Client Mode
FASTAPI_INROUTE_IS_SERVER Enable server mode true false
FASTAPI_INROUTE_IS_CLIENT Enable client mode false true
FASTAPI_INROUTE_SERVER_URL WebSocket server URL - wss://your-app.com/inroute
WEBSOCKET_FORWARD_SKIP_PATHS Paths to skip forwarding /inroute,/health,/docs -
WEBSOCKET_FORWARD_DEBUG Enable debug logging true (optional) true (optional)

Server Mode Endpoints

When running in server mode, these endpoints are automatically added:

  • /inroute - WebSocket endpoint for client connections
  • /connections - Monitor active client connections

Example

See the example/ directory for a complete working example with detailed instructions.

Documentation

Requirements

  • Python 3.11+
  • FastAPI 0.128.0+
  • websockets 16.0+

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details


Note: This package is designed for development and testing environments only. It should NOT be used for production traffic. For production deployments, use proper load balancers, API gateways, or direct webhook handling.

Made with Bob 🤖

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_inroute-0.1.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_inroute-0.1.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_inroute-0.1.0.tar.gz.

File metadata

  • Download URL: fastapi_inroute-0.1.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for fastapi_inroute-0.1.0.tar.gz
Algorithm Hash digest
SHA256 468f00416b3a61070ecf79c914c619b325f806cd2e9449e337893380d95a631b
MD5 febbb9111e3b8390f50da218e4b673d9
BLAKE2b-256 a5b1b182dd4afbd6a63dba7f2a26e1df8de84c52e43aee988b734e10f0b37a58

See more details on using hashes here.

File details

Details for the file fastapi_inroute-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_inroute-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e176c92012b62e7494cfb60c0721485e51cc787c31c95e75f6fc9707f9f8d364
MD5 c351b0834dbf42596489939fe7538e12
BLAKE2b-256 86c55671afebf06c798a327d0592ca6589fb97a37924d5f2d5a1d0b3270e8ea3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page