First Steps¶
Get started with FastHTTP in less than 2 minutes.
Installation¶
Install FastHTTP with pip:
For HTTP/2 support:
Your First Request¶
Create a file example.py:
from fasthttp import FastHTTP
from fasthttp.response import Response
app = FastHTTP()
@app.get(url="https://jsonplaceholder.typicode.com/posts/1")
async def main(resp: Response) -> dict:
return resp.json()
if __name__ == "__main__":
app.run()
Run it:
Output:
INFO | fasthttp | FastHTTP started
INFO | fasthttp | Sending 1 requests
INFO | fasthttp | GET https://jsonplaceholder.typicode.com/posts/1 200 234.56ms
INFO | fasthttp | Done in 0.24s
Important Requirement¶
Handler functions must have a return type annotation:
Without the -> dict annotation, the function will not work.
How It Works¶
When you call app.run(), FastHTTP:
- Collects all registered routes (functions with
@app.get,@app.post, etc.) - Validates each handler has type annotations
- Creates async tasks for all requests
- Executes requests in parallel using asyncio
- For each request:
- Applies dependencies
- Runs middleware.before_request()
- Checks security (SSRF, etc.)
- Sends HTTP request via httpx
- Runs middleware.after_response()
- Calls your handler function with Response
- Logs results and finishes
Key Concepts¶
- Route: Function decorated with
@app.get(),@app.post(), etc. - Handler: Function that processes the response
- Response: Object containing server's response (status, body, headers)
- Dependency: Function that modifies request config before sending
- Middleware: Plugin that can modify requests and responses globally
Next Steps¶
Continue learning:
- HTTP Methods - All supported HTTP methods
- Request Parameters - Query, JSON, headers
- Configuration - More settings