FastAPI Forward is a middleware that enables request forwarding/proxying in FastAPI applications. It allows you to easily forward specific routes to different target URLs while maintaining the original request context.
- Simple decorator-based route forwarding
- Preserves request headers and body
- Supports conditional forwarding
- Easy integration with FastAPI applications
pip install fastapi-forward
Here's a simple example that forwards requests from your FastAPI application to another service:
from fastapi import FastAPI
from fastapi_forward import forward, init_app
app = FastAPI()
init_app(app)
@app.get("/")
@forward("https://api.example.com")
async def root():
return {"message": "This response will be replaced by the forwarded response"}
In this example, any GET request to "/" will be forwarded to "https://api.example.com/".
The most basic usage is to forward a route to another URL:
@app.get("/users")
@forward("https://api.example.com")
async def get_users():
pass
All requests to "/users" will be forwarded to "https://api.example.com/users".
Make sure to initialize the middleware before using the forward decorator:
from fastapi import FastAPI
from fastapi_forward import init_app
app = FastAPI()
init_app(app)
The middleware:
- Preserves request headers
- Forwards request body
- Maintains HTTP methods
- Returns the response from the target URL
- Python 3.10
- FastAPI
- httpx >= 0.28.0
To set up the development environment:
# Install PDM if you haven't already
pip install pdm
# Install dependencies
pdm install
# Run tests
pdm run pytest
This project is licensed under the MIT License.