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.1.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.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_inroute-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 01cb3ad2cf0f89af01e0f1c7d4af848712f96a173f18219c1468e36372e25aa4
MD5 d4b0d58f2cd3fc7ba3710b3c20703dc5
BLAKE2b-256 bcd20ba2a1ea2909efceb5432d9c43d4a0045dccbf1115517005921e63abec9e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_inroute-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 481ec05ecb88686acd404f811155834c602ba7883787d8fb33ceb837ec2d69a7
MD5 a3873cc343612b19f5910c94aaf06906
BLAKE2b-256 35e45524054fe5a73b0f30a0737406468a19ce05431a0e1ce6d3366618a430fb

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