- Published on
Exploring FastAPI with My MathAPI Demo
- Authors
- Name
- Winston Brown
Exploring FastAPI with My MathAPI Demo
FastAPI has become my go-to choice for building APIs because of its simplicity, speed, and the automatic generation of interactive documentation. Recently, I created a small demo project called MathAPI, which calculates the average of a list of numbers through a FastAPI endpoint. In this blog post, I’ll walk you through this demo and share why I think FastAPI is such a fantastic tool for developers.
You can find the source code for MathAPI in my GitHub repository here: MathAPI GitHub Repository.
What is MathAPI?
MathAPI is a straightforward FastAPI application that accepts a list of numbers via a POST request and calculates their average. It’s a simple project, but it demonstrates how easy it is to build, run, and test APIs using FastAPI.
The API has just one endpoint: /average
. When you send a list of numbers in JSON format, MathAPI calculates and returns the average value. Here’s a quick example of how you can call it using curl
:
curl -X POST http://127.0.0.1:8000/average -H "Content-Type: application/json" -d '{"numbers": [1, 2, 3, 4]}'
This will return the average value of 2.5
. It’s that simple!
Setting Up MathAPI
To get started with MathAPI, you’ll need Python 3.6 or later. Here are the steps to set it up:
Clone the repository:
git clone https://github.com/winston-brown/mathapi_fastapi cd MathAPI
Install
pyenv
andpyenv-virtualenv
if you haven’t already:- Follow the pyenv installation guide.
- Install
pyenv-virtualenv
by following these instructions.
Set up the Python environment:
pyenv install 3.8.10 # You can use a different Python version if you prefer pyenv virtualenv 3.8.10 mathapi-env pyenv activate mathapi-env
Install dependencies:
This will install fastapi
and uvicorn
- an ASGI web server implemntation for Python
pip install -r requirements.txt
Run the API:
uvicorn main:app --reload
Once the server is running, you can access the API at http://127.0.0.1:8000.
FastAPI's Features in Action
One of the things I love about FastAPI is how it automatically generates interactive API documentation. When you run MathAPI, you can access this documentation by navigating to http://127.0.0.1:8000. This interface allows you to interact with the API directly in your browser, making testing and exploring endpoints incredibly convenient.
With MathAPI, you’ll see an automatically generated UI where you can test the /average
endpoint by providing a list of numbers and seeing the result without needing to write a single line of test code.
Why FastAPI?
FastAPI stands out for several reasons, which this small project helped me appreciate even more:
Ease of Use: The clean, Pythonic syntax makes FastAPI easy to understand and implement. Adding new routes or modifying existing ones is a breeze.
Speed: FastAPI, powered by Starlette and Pydantic, is designed for speed – not just the speed of serving requests, but also developer productivity. I was able to set up and run MathAPI in no time.
Automatic Documentation: I’ve mentioned it before, but the interactive documentation provided by OpenAPI and Swagger UI is a huge plus. It’s helpful for both development and for showcasing your API to others.
Type Checking: FastAPI takes full advantage of Python type hints, making it easy to validate incoming data. In MathAPI, FastAPI ensures the
numbers
list in the POST request is correctly formatted.
Conclusion
MathAPI is a small, simple demo, but it showcases just how fast you can spin up an API using FastAPI. From handling requests and responses to generating documentation, FastAPI takes care of a lot of the repetitive tasks we developers usually deal with.
If you’re interested, check out the project on GitHub, try running it locally, and see just how easy it is to build something functional with FastAPI. I’m planning to expand MathAPI to include more mathematical operations – perhaps even building a full-fledged calculation service.
Stay tuned for more updates on my journey with FastAPI, and feel free to reach out if you have questions or want to collaborate!