FastAPI is one of the fastest and most efficient frameworks for building APIs with Python. However, deploying a FastAPI application typically requires setting up a cloud server, which can be complex and time-consuming. In this guide, we'll show you how to bypass that hassle and instantly expose your FastAPI server to the internet using Pinggy—all with a single command!
What is FastAPI?
FastAPI is a modern web framework for building APIs quickly and efficiently. It leverages Python-type hints for automatic validation and documentation, making development smoother. Some of its standout features include:
- High Performance: Comparable to Node.js and Go in speed.
- Automatic Documentation: OpenAPI and Swagger UI are generated effortlessly.
- Built-in Validation: Ensures clean and error-free input handling.
- Asynchronous Support: Handles multiple requests efficiently using ASGI. Now, let’s start hosting a FastAPI application without a traditional server.
Running a FastAPI Application on Localhost
Step 1: Set Up a Virtual Environment
Using a virtual environment keeps your project dependencies isolated. To create and activate one:
python -m venv .venv
source .venv/bin/activate # For Linux/Mac
.venv\Scripts\Activate.ps1 # For Windows PowerShell
Step 2: Install FastAPI and Required Packages
Upgrade pip and install FastAPI with:
python -m pip install --upgrade pip
pip install "fastapi[standard]"
Step 3: Create a Simple FastAPI App
Create a main.py file with the following code:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
Step 4: Run the FastAPI Server
Start your FastAPI application locally:
uvicorn main:app --host 127.0.0.1 --port 8000
Now, you can access it at http://127.0.0.1:8000
.
Hosting FastAPI Online with Pinggy
Pinggy lets you expose your local FastAPI application to the internet instantly, without setting up a server.
Install and Start Pinggy
Run the following command to create a secure tunnel to your local FastAPI server:
ssh -p 443 -R0:localhost:8000 qr@a.pinggy.io
After executing this, Pinggy will generate a public URL like:
https://rnas-12-43-32-5.a.pinggy.link
Now, anyone with this URL can access your FastAPI application from anywhere!
Why Use Pinggy?
- Instant Deployment: No need for cloud setup—just one command.
- Easy Collaboration: Share your API with teammates or clients for testing.
- Secure Access: Supports HTTPS, so your connections are encrypted.
- Flexible Settings: Configure authentication, region selection, and more.
Understanding ASGI Servers for FastAPI
FastAPI runs on ASGI (Asynchronous Server Gateway Interface), which supports handling multiple concurrent requests efficiently. Here are some ASGI servers you can use:
1. Uvicorn
A lightweight and fast ASGI server, commonly used with FastAPI.
uvicorn main:app --host 127.0.0.1 --port 8000
2. Hypercorn
Supports HTTP/2 and QUIC protocols for advanced networking needs.
hypercorn main:app --bind 127.0.0.1:8000
3. Daphne
Great for real-time applications using WebSockets.
daphne -b 127.0.0.1 -p 8000 main:app
If needed, you can mix and match these servers for different functionalities in your FastAPI app.
Hosting an ASGI Server with Pinggy
If you're running your FastAPI app with Uvicorn or another ASGI server, you can still share it using Pinggy.
ssh -p 443 -R0:localhost:8000 qr@a.pinggy.io
This command will generate a public link for your ASGI server, making it accessible online instantly.
Conclusion:
FastAPI is a powerful framework for building APIs, and with Pinggy, you can share your local FastAPI app effortlessly. Whether you’re testing, demoing, or integrating with third-party services, Pinggy simplifies the process by eliminating the need for traditional servers or cloud hosting.
Top comments (1)
Interesting, thank you very much :-)