Agentbrisk

How to Use Replit Agent to Build and Deploy a Python API

May 6, 2026 · Editorial Team · 6 min read · replit-agentpythonfastapi

Building and deploying a Python API used to require a local environment, a cloud account, and a fair amount of config wiring. Replit Agent compresses all of that into a browser session. You describe the API, the agent scaffolds it, you test it in the same environment, and you deploy it to a live URL without leaving the tab.

This is particularly useful for prototypes, webhooks, and internal tools where you need a real endpoint fast but don't want to spend an hour on infrastructure. Here's how the whole process works.


Start a Replit Agent session

Go to replit.com and sign in. From the dashboard, click "Create Repl" and select "Agent" (or look for the "Build with AI" button; the exact label has changed a few times, but the Agent option is prominently placed as of 2026).

The Agent will ask you what you want to build. Unlike the standard Replit editor (where you write code directly), the Agent accepts a description and scaffolds the project for you.

A good starting prompt for a Python API:

Build a REST API in Python using FastAPI. The API should:
- Store and retrieve book records: title, author, ISBN, publication year
- Endpoints: GET /books, GET /books/{id}, POST /books, DELETE /books/{id}
- Use PostgreSQL for storage (via SQLAlchemy and Replit's built-in database)
- Validate input with Pydantic models
- Return proper HTTP status codes (404 for not found, 422 for validation errors)

The Agent will scaffold the project: it generates main.py, models.py, a requirements file, and the database setup code. This usually takes 60-90 seconds.


What Replit Agent sets up automatically

For a prompt like the one above, the Agent generates a complete project structure:

main.py          # FastAPI app with all routes
models.py        # SQLAlchemy models and Pydantic schemas
database.py      # Database connection and session management
requirements.txt # FastAPI, sqlalchemy, psycopg2-binary, pydantic

It also configures the Repl to use Replit's built-in PostgreSQL database. The connection string is injected via an environment variable (DATABASE_URL) that Replit manages automatically. You don't need to set this up yourself.

One thing worth checking after the initial scaffold: the Pydantic models. The Agent usually gets the field types right but sometimes misses Optional fields or uses str where you want int. Click on models.py in the file tree and review the schema before proceeding.


Running and testing the API

After scaffolding, click "Run." Replit installs the requirements and starts the FastAPI development server. In the right panel, you'll see the live output URL: something like https://yourproject.username.repl.co.

FastAPI includes automatic API documentation at /docs (Swagger UI). Navigate to https://yourproject.username.repl.co/docs to see all your endpoints, test them interactively, and verify the request/response schemas.

Test the basic flow from the docs UI:

  1. Expand POST /books. Click "Try it out."
  2. Enter a test book in the request body.
  3. Click "Execute." You should get a 201 response with the created record.
  4. Then test GET /books to verify the record is in the database.

If a request fails, click on the failing endpoint in the Replit console (the output panel at the bottom). FastAPI's error messages are specific; most issues at this stage are mismatched types in the Pydantic schema.


Adding more features via Agent chat

After the initial scaffold, the Agent chat panel (usually on the left side) stays active for follow-up requests. You can add features without switching to a code editor:

Add a search endpoint: GET /books/search?q=<query>
The query should match against title or author (case-insensitive).
Return an empty array if no results found, not a 404.

The Agent reads your existing code, adds the new endpoint, and updates any relevant models. The change shows up immediately in the editor and the running server restarts automatically.

For database migrations (adding a new column to an existing table), the Agent handles SQLAlchemy-based approaches. Be specific:

Add a 'rating' column to the Book model. 
Type: float, optional (nullable), range 0.0 to 5.0. 
Include it in the GET and POST endpoints.

One limitation: Replit Agent doesn't use Alembic for migrations by default. For development, it drops and recreates the table (which means any existing test data is lost). For a production database, you'd need to add Alembic manually after exporting the project.


Secrets management

Any credentials your API needs (external API keys, third-party service tokens, etc.) go in Replit's Secrets manager, not in your code.

Access it via the padlock icon in the left sidebar (or go to Tools > Secrets). Add a key-value pair, for example:

  • Key: OPENAI_API_KEY
  • Value: sk-...

In your Python code, access it with os.environ.get('OPENAI_API_KEY'). The Agent knows to use this pattern if you describe it correctly:

Add a POST /books/{id}/summary endpoint that calls the OpenAI API 
to generate a one-paragraph summary of the book. 
Use the OPENAI_API_KEY from environment variables.

The Agent will write the code using os.environ and not hardcode any keys. Secrets in Replit are encrypted at rest and not visible in the output or logs.

For the DATABASE_URL specifically: Replit injects this automatically for PostgreSQL repls. You don't need to add it to Secrets manually.


Deploying to a public URL

When you run the Repl, it's accessible via the URL in the top-right panel. This is a live URL, but it only stays active while someone is using the Repl. For a "always-on" deployment, you need Replit Deployments.

To deploy:

  1. Click "Deploy" in the top-right toolbar.
  2. Select "Reserved VM" for a persistent deployment (the cheapest tier is around $7/month as of 2026).
  3. Confirm the deployment settings. Replit will provision a dedicated container.
  4. Your API is now live at a permanent URL: https://yourproject.username.replit.app.

After deploying, the live deployment doesn't update automatically when you change code. To redeploy with new code, click Deploy again and confirm the new build. For active development, use the non-deployed Repl URL for testing and redeploy only when a version is stable.

Environment variables (Secrets) carry over to the deployment automatically. You don't need to reconfigure them.


Where Replit Agent works well and where it doesn't

The Agent is fast and produces working code for standard patterns: CRUD APIs, simple authentication, database operations, webhook handlers. It's particularly strong on FastAPI because it's a well-documented, popular framework that the model understands deeply.

Where it needs help:

Complex queries. Multi-table joins with specific performance requirements often need manual SQL or ORM tuning. The Agent produces correct queries but not necessarily efficient ones.

Background tasks. For anything involving Celery, Redis queues, or scheduled jobs, plan to write that code yourself after the initial scaffold. The Agent tends to inline async operations in ways that don't scale.

Large schemas. Asking for a 10-table API in one prompt produces a scaffold that usually has type inconsistencies between models. Build in stages: core tables first, then add related tables in separate requests.

Custom auth. JWT auth or OAuth flows work, but they require very specific prompting. Describe the exact flow you want (issue token on login, verify token via middleware, return 401 on invalid token) rather than just "add authentication."

Replit Agent is genuinely useful for the first version of a Python API. The combination of scaffolding, live environment, and one-click deployment is hard to beat for speed. The code it produces is maintainable enough to continue developing, and you own it completely: export it via git at any time.

Search