Skip to main content

FastAPI gRPC Bridge

A Python package that seamlessly adds gRPC support to FastAPI applications using decorators. It automatically generates Protocol Buffer definitions from Pydantic models and creates gRPC services that mirror your FastAPI routes.

Features

  • 🚀 Easy Integration: Add gRPC support to existing FastAPI apps with a simple decorator
  • 🔄 Auto Proto Generation: Automatically generates .proto files from Pydantic models
  • 📡 Dual Protocol Support: Serve both HTTP and gRPC from the same codebase
  • ⚡ Auto-Start gRPC: Automatically starts gRPC server when FastAPI starts (configurable)
  • 🎯 Type Safety: Full type safety with Pydantic models
  • 🔧 FastAPI Router Support: Works with FastAPI routers and sub-applications
  • 🌐 Async Support: Full support for async/await functions

Installation

pip install fastapi-grpc-bridge

Quick Start

from fastapi import FastAPI
from fastapi_grpc_bridge import grpc_route, add_grpc_support
from pydantic import BaseModel

app = FastAPI()

class HelloResponse(BaseModel):
    message: str

@app.get("/hello")
@grpc_route("/hello")
async def say_hello(name: str) -> HelloResponse:
    return HelloResponse(message=f"Hello, {name}!")

# Add gRPC support - this will auto-start the gRPC server when FastAPI starts
add_grpc_support(app)

if __name__ == "__main__":
    import uvicorn
    # Both HTTP (port 8000) and gRPC (port 50051) will start automatically
    uvicorn.run(app, host="0.0.0.0", port=8000)

2. Manual gRPC Server Control

from fastapi import FastAPI
from fastapi_grpc_bridge import grpc_route, add_grpc_support, start_grpc_server_standalone
from pydantic import BaseModel

app = FastAPI()

class HelloResponse(BaseModel):
    message: str

@app.get("/hello")
@grpc_route("/hello")
async def say_hello(name: str) -> HelloResponse:
    return HelloResponse(message=f"Hello, {name}!")

# Disable auto-start
add_grpc_support(app, auto_start_grpc=False)

# Start gRPC server manually in a separate script
if __name__ == "__main__":
    start_grpc_server_standalone()  # Or use this for standalone gRPC server

3. Start Both Services

Option A: Auto-Start (Single Command)

python app.py
# This starts both FastAPI (HTTP) on port 8000 and gRPC on port 50051

Option B: Manual Start (Separate Commands)

# Terminal 1: Start FastAPI
uvicorn app:app --host 0.0.0.0 --port 8000

# Terminal 2: Start gRPC server
python -c "from app import app; from fastapi_grpc_bridge import start_grpc_server_standalone; start_grpc_server_standalone()"

Working with FastAPI Routers

from fastapi import FastAPI, APIRouter
from fastapi_grpc_bridge import grpc_route, add_grpc_support
from pydantic import BaseModel

app = FastAPI()
api_router = APIRouter(prefix="/api")

class UserResponse(BaseModel):
    id: int
    name: str
    email: str

@api_router.get("/user")
@grpc_route("/user")
async def get_user(user_id: int) -> UserResponse:
    return UserResponse(
        id=user_id,
        name=f"User {user_id}",
        email=f"user{user_id}@example.com"
    )

app.include_router(api_router)
add_grpc_support(app)  # Auto-starts gRPC server

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

Type Support

The package automatically maps Python types to Protocol Buffer types:

Python Type Protocol Buffer Type
str string
int int32
float float
bool bool
bytes bytes
Optional[T] T (optional)

API Reference

@grpc_route(path: str, response_model: Optional[Type[BaseModel]] = None)

Decorator to add gRPC support to FastAPI routes.

Parameters:

  • path: Route path (used for naming the gRPC service)
  • response_model: Pydantic model for the response (optional if return annotation is provided)

add_grpc_support(app_or_router, auto_start_grpc=True)

Add gRPC support to a FastAPI app or router.

Parameters:

  • app_or_router: FastAPI app or APIRouter instance
  • auto_start_grpc: Whether to automatically start gRPC server when FastAPI starts (default: True)

start_grpc_server_standalone()

Start the gRPC server in standalone mode (for manual control).

Examples

See the examples/ directory for complete working examples:

  • examples/app.py: Complete example with auto-start functionality
  • examples/start_grpc_server.py: Standalone gRPC server launcher
  • examples/test_auto_start.py: Test script to verify both services

Testing Your Services

Test HTTP Service

curl "http://localhost:8000/hello?name=World"
curl "http://localhost:8000/api/user?user_id=123"
curl "http://localhost:8000/health"  # Shows gRPC routes status

Test gRPC Service

import grpc
import sys
sys.path.append('generated_protos')

import say_hello_pb2
import say_hello_pb2_grpc

def test_grpc_service():
    channel = grpc.insecure_channel('localhost:50051')
    stub = say_hello_pb2_grpc.Say_helloServiceStub(channel)
    
    request = say_hello_pb2.say_helloRequest(name="World")
    response = stub.Call(request)
    
    print(f"Response: {response.message}")
    channel.close()

if __name__ == "__main__":
    test_grpc_service()

Automated Testing

# Start the app
python examples/app.py &

# Run tests
python examples/test_auto_start.py

Generated Files

The package automatically generates:

  • .proto files in the generated_protos/ directory
  • Python gRPC client and server stubs (*_pb2.py and *_pb2_grpc.py)

Configuration

Ports

  • HTTP: 8000 (configurable via uvicorn)
  • gRPC: 50051 (fixed, can be modified in grpc_server.py)

Auto-Start Behavior

  • Default: gRPC server auto-starts when FastAPI starts
  • Disable: Set auto_start_grpc=False in add_grpc_support()
  • Manual: Use start_grpc_server_standalone() for manual control

Requirements

  • Python 3.7+
  • FastAPI
  • Pydantic
  • grpcio
  • grpcio-tools
  • requests (for testing)

License

MIT License

Metadata

Release files for fastapi-grpc-bridge 0.1.2

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

Source distribution (sdist)

Source distribution for fastapi-grpc-bridge 0.1.2
File Size Uploaded
fastapi_grpc_bridge-0.1.2.tar.gz 13.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-grpc-bridge 0.1.2
File Interpreter ABI Platform
fastapi_grpc_bridge-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 27.7 kB

Release files / fastapi_grpc_bridge-0.1.2.tar.gz

Download URL fastapi_grpc_bridge-0.1.2.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
bec7bef26789e8f7ad9e0af8e70f093933e87c9d0085bc615e52daa35527bfd8
BLAKE2b-256 checksum
How to use checksums
998948dcb63538cc7b3ed709911d2f59c8ef22c44bca63d223e3ca9d0b901e6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.16

Release files / fastapi_grpc_bridge-0.1.2-py3-none-any.whl

Download URL fastapi_grpc_bridge-0.1.2-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9389988706b2b825042028e04519ec3ec8c6ce4281bd1c0a655770d1ac787c29
BLAKE2b-256 checksum
How to use checksums
db5adad50caa3ae5cc51d1c8be96c0fcdbb6a1181c5062d59ea0266114ab982d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.16

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

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