---
title: "Deploy FastAPI over MCP with while.sh"
url: https://while.sh/en/guides/deploy-fastapi/
author: "Aloïs Oddon, co-founder of Nextmap"
updated: 2026-11-02
---

# Deploy a FastAPI API from your AI agent, over MCP

Your AI agent wrote your FastAPI API. Connected to while over MCP, it also deploys it, over HTTPS, with no Dockerfile or nginx to configure.

## Prerequisites

- A FastAPI API, with a requirements.txt file.
- An AI agent that works in a terminal: Claude Code, Codex, Cursor or Gemini CLI.

## Step 1: Give your agent these instructions

Open your agent in your API's folder, and paste these instructions into it:

```text titre="Instructions"
Connect to while with my account (while.sh/agents), then deploy my app.
```

Without an account, your agent deploys your API as a trial, in one request. To keep it, open the link it gives you and create your account.

With your account, your agent adds while's MCP server itself. If it asks you to, restart it. On the first call to while, a page opens in your browser. In Claude Code, if it doesn't open, type /mcp, choose while, then Authenticate. On that page, choose your app, or "A new app", then click Authorize.

## Step 2: Open your API

Your agent gives you your API's address, for example `https://mon-api.while.cloud`, over HTTPS.

## What your agent does

It writes while.yaml at the root of your API:

```yaml titre="while.yaml"
apiVersion: while/v1
project: mon-api
services:
  - name: web
    port: 8000
    health:
      path: /
```

Port 8000 is the default of uvicorn, FastAPI's server (source 2). Your API is installed, its tests run, then it starts, with no command to write. Render's example FastAPI app deploys on while as is (source 1).

## Add a database

> You, to your agent: Add a PostgreSQL database.

Your agent declares the database in while.yaml, and its address reaches your code in `DATABASE_URL`. It is backed up every day, and can go back to a precise moment if needed.

## Run a job every night

> You, to your agent: Run the data import every night at 3 am.

Your agent declares a scheduled job, which starts at the set time with the same code and the same variables. If it fails, your agent is notified.

## Restrict your API to your colleagues

> You, to your agent: Restrict the API to people in the company.

Your agent adds two lines to the service:

```yaml titre="while.yaml"
    access: internal
    email_domains: [acme.com]
```

Once your Google Workspace or Microsoft 365 is linked to while, on the Pro plan, your colleagues open the API with their usual account, and your code receives their name and email address, with no sign-in page to write.

## Move to your own domain

> You, to your agent: Put the API on api.acme.com.

Your agent declares the domain, you add the DNS records it gives you, and the HTTPS certificate follows.

## See a new version before you deploy it

> You, to your agent: Show me the new version before you deploy it.

Your agent opens a preview, at its own address, with its own data. You look at it, then your agent puts it into production.

## Fix a vulnerability in your dependencies

> You, to your agent: Fix the vulnerabilities in our dependencies.

Your API's dependencies are checked against known vulnerabilities at every deployment, then every day. Your agent is warned about a serious vulnerability, and updates the package involved.

## Go back to the previous version

> You, to your agent: Go back to the previous version.

In the 30 minutes after a deployment, the previous version comes back in a second. After that, it takes a few seconds. Once the previous version is back, your agent reads what broke in while's logs, fixes it and deploys again.

## Next steps

- [Deploy a Node.js API and its PostgreSQL database](/en/guides/deploy-node-api-postgresql/)
- [Run a scheduled job](/en/guides/run-cron-job/)
- [Compare the hosts that have an MCP server](/en/comparisons/mcp-hosting-claude-code/)

## Sources

1. [render-examples/fastapi, an example FastAPI API published on GitHub](https://github.com/render-examples/fastapi), 2026-11-02
2. [Uvicorn, server settings (--port, 8000 by default)](https://uvicorn.dev/settings/), 2026-11-02

## Questions

### Do I need to write a Dockerfile?

No. Your Python API is detected, installed from requirements.txt and started with uvicorn when its main file, main.py for example, is at the root. Otherwise, your agent writes the command.

### Do my tests run?

Yes, pytest runs at every deployment, and a version that fails doesn't replace the previous one.

### Can I call an external API without exposing its key?

Yes. You enter the key in while, and your API uses it without ever reading it.
