To provide real-time weather data, you need to handle API integration, error handling and automatic scaling. With Vercel functions and FastAPI, you can build a production-ready weather API in minutes.
In this tutorial, you will build and deploy a weather API route using the Open-Meteo API that:
- Accepts a city name and geocodes it to coordinates
- Fetches current weather data from the Open-Meteo API
- Returns temperature, humidity, wind speed, and other weather metrics with optional metric/imperial unit conversion
- Python 3.10+ installed locally
- A Vercel account
- Basic understanding of FastAPI and async/await concepts
Start with the template Deploy FastAPI Boilerplate. Once your the project is deployed on Vercel, clone the repository locally.
You can also create the project locally using the Vercel CLI with the following command
Update your requirements.txt to include the httpx library for making async HTTP requests:
Open main.py and add the necessary imports at the top of the file:
Then add the weather endpoint after your existing routes:
Install the dependencies and launch the application in dev mode:
Use curl to test the API route:
You can also test the endpoint in your browser using the interactive API documentation at http://localhost:3000/docs.
- Push the changes to your remote repository or run the
vercelcli command - Vercel will create a new preview deployment for your to test
- Merge to main branch to deploy to Production
When you deploy a FastAPI app to Vercel, the application becomes a single Vercel Function and uses Fluid compute by default.
In this tutorial, you’ve built a real-time weather API using FastAPI on Vercel.
You learned to:
- Structure a dynamic API route and integrate external APIs
- Deploy the app to Vercel as a function for automatic scaling
For a production application, make sure that you use a weather API that will not be rate limited based on the amount of traffic that you are expecting.