> ## Documentation Index
> Fetch the complete documentation index at: https://docs.valyent.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# FastAPI

> Learn how to deploy an FastAPI application to Valyent

## Introduction

[FastAPI](https://fastapi.tiangolo.com) is a modern, fast (high-performance), web framework for building APIs with Python 3.8+ based on standard Python type hints.

## Prerequisites

* a UNIX-like system (see [WSL](https://learn.microsoft.com/en-us/windows/wsl/install) if you are using Windows), in order to use the [Valyent CLI](https://docs.valyent.cloud/cli)
* a Valyent account, with a registered payment method (you can sign up [here](https://console.valyent.cloud/auth/sign-up))
* [Python3](https://www.python.org) and installed on your machine

## Installing the CLI

```bash theme={"system"}
curl -L https://cli.valyent.cloud | sh
```

You can inspect the installation script [here](https://cli.valyent.cloud) and the [GitHub repository](https://github.com/valyentdev/cli).

## Authenticating

```bash theme={"system"}
valyent auth login
```

## Initializing the project

In the first place, we are going to install FastAPI dependencies.
You can refer to the [official documentation](https://fastapi.tiangolo.com/tutorial/#install-fastapi) for more information.

```bash theme={"system"}
pip install "fastapi[standard]"
```

Then, we can scaffold a new FastAPI project :

```bash theme={"system"}
mkdir fastapi-app && cd fastapi-app && touch main.py
```

Make sure to create a `requirements.txt` file, in order to install the dependencies in the Docker image :

```bash theme={"system"}
pip freeze > requirements.txt
```

In the `main.py` file, we can add the following code :

```bash theme={"system"}
# main.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
  return {"Hello": "World"}
```

Now, we can initialize the `valyent.toml` configuration file, that allows to link the local codebase, to a Valyent application :

```bash theme={"system"}
valyent init
```

This command will ask you to select/create a project and an associated application.

## Adding the Dockerfile

Let's add a Dockerfile so that Valyent can build a Docker image for your application.

```bash theme={"system"}
touch Dockerfile
```

We can fill it with the following Dockerfile (coming from [here](https://fastapi.tiangolo.com/deployment/docker/#dockerfile)) :

```bash theme={"system"}
# Stage 1: Builder
FROM python:3.13 AS builder

WORKDIR /code

# Install dependencies
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# Stage 2: Runtime
FROM python:3.13-slim AS runtime

WORKDIR /code

# Copy only necessary files from the builder stage
COPY --from=builder /usr/local/lib/python3.13/site-packages /usr/local/lib/python3.13/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# Copy the application code
COPY ./ /code/

CMD ["fastapi", "run", "--host", "0.0.0.0", "--port", "80"]
```

## Deploy the project

To expose the application’s port, we need to set up a gateway (in this case, the port 80):

```bash theme={"system"}
valyent gateways create
```

Then, we can deploy the application :

```bash theme={"system"}
valyent deploy
```

Then, you can type the following:

```bash theme={"system"}
valyent open
```

to test your FastAPI application in your browser.
