> ## Documentation Index
> Fetch the complete documentation index at: https://codegeninc-codegen-bot-sdk-docs-2-0.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Migrating from Flask to FastAPI

Migrating from [Flask](https://flask.palletsprojects.com/) to [FastAPI](https://fastapi.tiangolo.com/) involves several key changes to your codebase. This guide will walk you through using Codegen to automate this migration, handling imports, route decorators, static files, and template rendering.

You can find the complete example code in our [examples repository](https://github.com/codegen-sh/codegen-sdk/tree/develop/codegen-examples/examples/flask_to_fastapi_migration)

## Overview

The migration process involves four main steps:

1. Updating imports and initialization
2. Converting route decorators
3. Setting up static file handling
4. Updating template handling

Let's walk through each step using Codegen.

## I: Update Imports and Initialization

First, we need to update Flask imports to their FastAPI equivalents and modify the app initialization:

<Tip>
  Learn more about [imports here](/building-with-codegen/imports).
</Tip>

```python
from codegen import Codebase

# Parse the codebase
codebase = Codebase("./")

# Update imports and initialization
for file in codebase.files:
    # Update Flask to FastAPI imports
    for imp in file.imports:
        if imp.name == "Flask":
            imp.set_name("FastAPI")
        elif imp.module == "flask":
            imp.set_module("fastapi")

    # Update app initialization
    for call in file.function_calls:
        if call.name == "Flask":
            call.set_name("FastAPI")
            # Remove __name__ argument (not needed in FastAPI)
            if len(call.args) > 0 and call.args[0].value == "__name__":
                call.args[0].remove()
```

This transforms code from:

```python
from flask import Flask
app = Flask(__name__)
```

to:

```python
from fastapi import FastAPI
app = FastAPI()
```

<Note>
  FastAPI doesn't require the `__name__` argument that Flask uses for template
  resolution. Codegen automatically removes it during migration.
</Note>

## II: Convert Route Decorators

Next, we update Flask's route decorators to FastAPI's operation decorators:

```python
for function in file.functions:
    for decorator in function.decorators:
        if "@app.route" in decorator.source:
            route = decorator.source.split('"')[1]
            method = "get"  # Default to GET
            if "methods=" in decorator.source:
                methods = decorator.source.split("methods=")[1].split("]")[0]
                if "post" in methods.lower():
                    method = "post"
                elif "put" in methods.lower():
                    method = "put"
                elif "delete" in methods.lower():
                    method = "delete"
            decorator.edit(f'@app.{method}("{route}")')
```

This converts decorators from Flask style:

```python
@app.route("/users", methods=["POST"])
def create_user():
    pass
```

to FastAPI style:

```python
@app.post("/users")
def create_user():
    pass
```

<Tip>
  FastAPI provides specific decorators for each HTTP method, making the API more
  explicit and enabling better type checking and OpenAPI documentation.
</Tip>

## III: Setup Static Files

FastAPI handles static files differently than Flask. We need to add the StaticFiles mounting:

```python
# Add StaticFiles import
file.add_import("from fastapi.staticfiles import StaticFiles")

# Mount static directory
file.add_symbol_from_source(
    'app.mount("/static", StaticFiles(directory="static"), name="static")'
)
```

This sets up static file serving equivalent to Flask's automatic static file handling.

<Note>
  FastAPI requires explicit mounting of static directories, which provides more
  flexibility in how you serve static files.
</Note>

## IV: Update Template Handling

Finally, we update the template rendering to use FastAPI's Jinja2Templates:

```python
for func_call in file.function_calls:
    if func_call.name == "render_template":
        # Convert to FastAPI's template response
        func_call.set_name("Jinja2Templates(directory='templates').TemplateResponse")
        if len(func_call.args) > 1:
            # Convert template variables to context dict
            context_arg = ", ".join(
                f"{arg.name}={arg.value}" for arg in func_call.args[1:]
            )
            func_call.set_kwarg("context", f"{'{'}{context_arg}{'}'}")
        # Add required request parameter
        func_call.set_kwarg("request", "request")
```

This transforms template rendering from Flask style:

```python
@app.get("/users")
def list_users():
    return render_template("users.html", users=users)
```

to FastAPI style:

```python
@app.get("/users")
def list_users(request: Request):
    return Jinja2Templates(directory="templates").TemplateResponse(
        "users.html",
        context={"users": users},
        request=request
    )
```

<Note>
  FastAPI requires the `request` object to be passed to templates. Codegen
  automatically adds this parameter during migration.
</Note>

## Running the Migration

You can run the complete migration using our example script:

```bash
git clone https://github.com/codegen-sh/codegen-sdk.git
cd codegen-examples/examples/flask_to_fastapi_migration
python run.py
```

The script will:

1. Process all Python [files](/api-reference/python/PyFile) in your codebase
2. Apply the transformations in the correct order
3. Maintain your code's functionality while updating to FastAPI patterns

## Next Steps

After migration, you might want to:

* Add type hints to your route parameters
* Set up dependency injection
* Add request/response models
* Configure CORS and middleware

Check out these related tutorials:

* [Increase Type Coverage](/tutorials/increase-type-coverage)
* [Managing TypeScript Exports](/tutorials/managing-typescript-exports)
* [Organizing Your Codebase](/tutorials/organize-your-codebase)

## Learn More

* [FastAPI Documentation](https://fastapi.tiangolo.com/)
* [Codegen API Reference](/api-reference)
* [Moving Symbols Guide](/building-with-codegen/moving-symbols)
* [Dependencies and Usages](/building-with-codegen/dependencies-and-usages)
