Issue with routing for Python ASGI app

Hello!

I am trying to deploy an app with a React frontend and Python/FastApi backend. I have a vercel.json config file to point all requests to /api/* to main.py, but am consistently getting 404s trying to hit the API.

{
    "routes": [
        {
            "src": "/api/(.*)",
            "dest": "/api/main.py"
        }
    ]
}

Project structure

my_repo/
├── api/
│   ├── __init__.py
│   ├── auth.py
│   ├── database.py
│   ├── db.py
│   ├── main.py
│   ├── models.py
│   ├── settings.py
│   ├── test.py
│   └── venv/
├── app/
│   ├── index.html
│   ├── package.json
│   ├── package-lock.json
│   ├── src/
│   │   └── index.tsx
│   ├── venv/
│   └── vite.config.ts
├── LICENSE
├── README.md
├── login_plan.md
├── package.json
├── requirements.txt
├── vercel.json
└── venv/

Would appreciate any pointers as to what I’m missing, thanks!

Instead of the “routes” key you’ll need to use “rewrites” here

Thank you @jacobparis - I’ve tried a couple iterations and am still missing something. Is the destination syntax for the python app correct or it is only supposed to reflect an http URL?

{
    "builds": [
        {
            "src": "api/main.py",
            "use": "@vercel/python"
        },
        {
            "src": "package.json",
            "use": "@vercel/static-build",
            "config": {
                "distDir": "app/dist"
            }
        }
    ],
    "rewrites": [
        {
            "source": "/api/(.*)",
            "destination": "/api/main.py"
        },
        {
            "source": "(.*)",
            "destination": "app/dist/$1"
        }
    ]
}

This looks correct, I checked out your codebase (since it’s a public repo) and the issue looks like your VITE_API_URL environment variable. If I replace that with a hardcoded /api then the routes map to /api/scary which correctly hits the python function

They currently have a fallback to http://locahost:8000 which does not end up invoking the /apiroutes

Notably I do get a 500 error after that but atm I am assuming that’s because I don’t have any of the other integrations set up or running