Skip to content

Asynchronous Server Gateway Interface (ASGI)

In Python, the Asynchronous Server Gateway Interface (ASGI) is a standard contract between async web servers and the web applications or frameworks they run. Any ASGI server, such as Uvicorn, Hypercorn, or Daphne, can host any ASGI app, including FastAPI, Starlette, and Django’s async stack.

An ASGI application is a single coroutine function that takes three arguments: scope, receive, and send. The scope is a dictionary describing the connection, with a type key such as "http", "websocket", or "lifespan". The app awaits receive() to get incoming event dictionaries and awaits send() to push events back to the server. Here’s how a single HTTP request flows through that exchange:

The server turns request bytes into a call to app(scope, receive, send), and the app awaits receive() and send() to exchange event dicts.
One HTTP Request as ASGI Events

ASGI is the async successor to WSGI (Web Server Gateway Interface), which ties an app to one synchronous request and response. Because every exchange is an awaitable event, one ASGI app can serve long-lived WebSocket connections and many concurrent requests on a single event loop.

Example

Say you want to see what an ASGI server actually exchanges with your app, without installing a server. You can write a tiny echo app and play the server’s role yourself:

Language: Python
>>> import asyncio

>>> async def app(scope, receive, send):
...     event = await receive()
...     reply = f"{scope['method']} {scope['path']}: ".encode() + event["body"]
...     await send({
...         "type": "http.response.start",
...         "status": 200,
...         "headers": [(b"content-type", b"text/plain")],
...     })
...     await send({"type": "http.response.body", "body": reply})
...

>>> async def receive():
...     return {"type": "http.request", "body": b"ping", "more_body": False}
...

>>> async def send(event):
...     print(event["type"], event.get("status", event.get("body")))
...

>>> scope = {"type": "http", "method": "POST", "path": "/echo"}
>>> asyncio.run(app(scope, receive, send))
http.response.start 200
http.response.body b'POST /echo: ping'

The app never touches a socket. It reads the method and path from scope, awaits receive() for the request body, and answers with two events. The first carries the status and headers, and the second carries the body. A real server like Uvicorn fills scope with more keys, such as headers and query_string. It also plays the part of the fake receive() and send(), turning network bytes into events and back.

Get Started With FastAPI

Tutorial

Get Started With FastAPI

FastAPI is the first choice when creating APIs in Python. Explore FastAPI code examples and get the most frequent questions about FastAPI answered.

intermediate api front-end web-dev

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Martin Breuss • Updated Nov. 9, 2026