My project works locally but fails to run on Vercel

Hello everyone,
I am using Python and FastAPI to deploy an API using vercel. The fastapi is working on my local host 127.0.0.1:8000 but it is not working on vercel. I have used this approach on a different project also and it worked for that project (same tech stack)


Ideally for /100/?handle=Amritanshu_Barpanda&resource_id=1, it should display something like
image

This is the code - GitHub - ABarpanda/Next-Problem
Deployment - https://next-problem.vercel.app/

Any help would be really appreciated.

Hello,

you need to make sure the Python code is inside /api folder so our build system can generate it as Serverless function. You can get started with our FastAPI template

  1. I am not using Node.js in my project as of now.
  2. Even after making the suggested changes, the problem is not resolved. (I have updated my changes in the GitHub repo. Please take a look.)
  3. Now it is showing 404 error instead of 500 error

Hey @abarpanda, can you update your vercel.json file as shown below? Also, please add python-dotenv==<version> to your requirements.txt file.

{
  "version": 2,
  "builds": [
    {
      "src": "api/main.py",
      "use": "@vercel/python"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "api/main.py"
    }
  ]
}

You can also simply rename your main.py file to index.py and to avoid using custom config in vercel.json - Upgrading legacy routes

Thank you so much @vikhyathsudembala-ve , that solved my problem. I have successfully deployed the project on vercel. I guess the problem was with the python-dotenv==<version>.

I have one more small doubt -

In my local machine, the (correct) output is -

While in my deployed link-

  1. From your experience, do you know why this may be happening?
  2. Since this is not related to this thread or Vercel in particular, is this the correct place to ask about this?

Thank you everyone for your time. (especially @swarnava and @vikhyathsudembala-ve )
I have solved the issue. It was being caused because I was trying to save the data to a file as part of my previous data backup and debugging efforts. (rookie error)

Thanks again Vercel. It has been a great experience. :heart:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.