Skip to content

Getting started with BlackSheep

This tutorial explains how to create and start a minimal BlackSheep web application.
It provides a general view, covering the following topics:

  • Creating a web application from scratch.
  • Running the web application.
  • Configuring routes.
  • Handling parameters.
  • Handling responses.


  • Python version 3.7, 3.8, or 3.9
  • path to the python executable configured in the environment $PATH variable (tip: if you install Python on Windows using the official installer, enable the checkbox to update your $PATH variable during the installation)

Preparing a development environment

Create a folder in the desired location on your file system, then open a command line terminal and navigate to the new folder. Create a virtual environment using the following command:

python -m venv venv

and activate it:

# on Linux or Mac:
source venv/bin/activate

# on Windows:

BlackSheep belongs to the category of ASGI web frameworks, therefore it requires an ASGI HTTP server to run, such as uvicorn, daphne, or hypercorn. For this tutorial, install uvicorn together with blacksheep:

pip install blacksheep uvicorn

Creating and running a web application

Create a file, and paste the following contents into it:

from datetime import datetime
from blacksheep.server import Application

app = Application()

def home():
    return f"Hello, World! {datetime.utcnow().isoformat()}"

Use the command below to start the application using port 44777, with automatic reload on file change:

uvicorn server:app --port 44777 --reload

The terminal should display information like in the picture below:

Run application

Open a web browser and navigate to The web browser will display the text answer from the web application:

Hello World

Configuring routes

The current code configures a request handler for HTTP GET method at the root path of the application: "/". Note how a function decorator is used to register the home function as request handler:

def home():

This means that whenever a HTTP GET request is received at the root URL of the application (e.g., the home function is used to handle the request and produce a response.

The application object provides two ways of defining routes:

  1. using app.route method
  2. using app.router's methods

The first way enables defining request handlers specifying a path, and optional HTTP methods (defaults to "GET" only). The second way provides methods for each HTTP method.

# Handles HTTP GET and HEAD requests at /some-route
@app.route("/some-route", methods=["GET", "HEAD"])
def mix_example():

# Handles HTTP GET at /
def example_get():

# Handles HTTP POST at /"/")
def example_post():

# Handles HTTP DELETE at /
def example_delete():

To reduce code verbosity when defining routes, it is possible to assign router methods to variables. Edit the previous file to replace its contents with the following:

from blacksheep.server import Application

app = Application()
get = app.router.get
post =

def home(request):
    return "GET Example"

def post_example(request):
    return "POST Example"

Note: Thanks to uvicorn's auto reload feature (used with --reload argument), when the file is updated, the application is automatically reloaded. This is extremely useful during development.

Navigate again to, it should display the text: "GET Example".

To verify that the post_example request handler is handling POST requests, use a tool to generate a POST HTTP request at the server's address. For example, using curl:

curl -X POST

Or PowerShell:

Invoke-WebRequest -Uri http://localhost:44777 -Method POST

Note: the application automatically handles requests for any path that is not handled by the router, returning an HTTP 404 Not Found response; and returns HTTP 500 Internal Server Error in case of unhandled exceptions happening during code execution.

Handling route parameters

So far the examples only showed request handlers that didn't use any input parameter. To define a request handler that uses a route parameter, define dynamic routes using the following syntax:

def greetings(name):
    return f"Hello, {name}!"

Route parameters and function parameter are bound by matching name. Add the fragment of code above to and try navigating to

A route can contain several named parameters, separated by slashes, and dynamic fragments mixed with static fragments:

def multiple_parameters(one, two, three):
    return f"1: {one}, 2: {two}, 3: {three}!"

def mix(movie_id, actor_id):

Route parameters are by default treated as strings, but BlackSheep supports automatic parsing of values, when function arguments are annotated using built-in typing annotations. For example, to define a route that handles integer route parameters and returns HTTP 400 Bad Request for invalid values, it is sufficient to decorate the function argument this way:

def only_numbers_here(number: int):
    return f"Lucky number: {number}\n"
Lucky number: 777
Bad Request: Invalid value ['x'] for parameter `number`; expected a valid int.

Several built-in types are handled automatically: e.g. str, bool, int, float, uuid.UUID,, datetime.datetime, List[T], Set[T].

Handling query string parameters

In the same way route parameters are injected automatically into request handlers by route parameters with matching names, blacksheep can handle query string parameters automatically. Adds this new fragment to your application:

def query_greetings(name: str):
    return f"Hello, {name}!"

Then navigate to http://localhost:44777/query?name=World.

A request handler can use different query strings and query string parameters support lists.

from typing import List

def greetings_many(name: List[str]):
    return f"Hello, {', '.join(name)}!"

# example:
# http://localhost:44777/query-list?name=Charlie&name=Lisa&name=Snoopy
# --> Hello, Charlie, Lisa, Snoopy!

Every handler can have many input parameters from different sources: request headers, cookies, query, route, request body, configured application services. These are treated in more details in the dedicated page about Binders.

Accessing the request object

To access the HTTP Request object directly, add a parameter called "request" to the signature of a request handler (type annotation is optional):

from blacksheep.messages import Request

def request_object(request: Request):
    # the request object exposes methods to read headers, cookies,
    # body, route parameters

This subject will be treated in more details in a different section.

Handling responses

Request handlers must return an instance of blacksheep.messages.Response class. The module blacksheep.server.responses provides several functions to produce responses.

The following example shows how to serve a JSON response, using a class defined with dataclass. Delete all contents from the current file and paste the following code:

from dataclasses import dataclass
from uuid import UUID, uuid4

from blacksheep.server import Application
from blacksheep.server.responses import json

class Cat:
    id: UUID
    name: str
    active: bool

app = Application()
get = app.router.get

def get_cats():
    return json(
            Cat(uuid4(), "Lampo", True),
            Cat(uuid4(), "Milady", True),
            Cat(uuid4(), "Meatball", True),
            Cat(uuid4(), "Pilou", True),

Then navigate to to see the result, it will look like this:


Note: try also the pretty_json function in blacksheep.server.responses.

For more granular control, it is possible to use the blacksheep.messages.Response class directly (read blacksheep.server.responses module for examples), and it is possible to modify the response before returning it to the client: for example to set a response header.

def get_cats():
    response = json(
            Cat(uuid4(), "Lampo", True),
            Cat(uuid4(), "Milady", True),
            Cat(uuid4(), "Meatball", True),
            Cat(uuid4(), "Pilou", True),

    response.add_header(b"X-Foo", b"Foo!")

    return response

Asynchronous request handlers

The examples so far show synchronous request handlers. To define asynchronous request handlers, define async functions:

async def get_movies():
    # ... do something async (example)
    movies = await movies_provider.get_movies()
    return json(movies)

Asynchronous code is described more in other sections of the documentation.


This tutorial covered the ABCs of creating a BlackSheep application. The general concepts presented here apply to any kind of web framework:

  • server side routing
  • handling of query strings and route parameters
  • handling of requests and responses

The next page will describe a more articulated scenario, including handling of HTML views on the server side, serving static files, and more.