**Introduction**
You have trained a machine learning model. You spent weeks tuning it. You got great results. Now what? You need to share your model with other people. You need to let them use it. That is where APIs come in.
An API is a tool that lets your model talk to other software. Think of it like a restaurant. Your model is the kitchen. The API is the waiter. The waiter takes orders from customers and brings them to the kitchen.
The kitchen sends back food. The API sends back predictions from your model.
You have two main choices to build your API. Flask and FastAPI are the top picks. Flask is simple and fast to set up. You can build something quick for testing. FastAPI is newer and much faster.
FastAPI handles over 20,000 requests per second. Flask handles about 4,000 to 5,000 requests per second. FastAPI also checks your data automatically. It catches mistakes before they cause problems.
Most teams follow the same path. You train your model on your computer. You save it using pickle or joblib. You wrap it in an API using FastAPI or Flask. You put it in a Docker container.
Then you send it to the cloud on Heroku or AWS. This process works for real jobs.
Your choice matters. FastAPI works best for production work. Flask works best for quick tests and small projects inside your company. Both use Python. Both are free. Both let you serve predictions to anyone, anywhere.
Let’s build your first API.
Key Takeaways
- FastAPI can serve over 20,000 requests per second. Flask serves around 4,000–5,000 requests per second. FastAPI is better for high traffic and async calls; Flask is good for simple or smaller apps.
- Save your machine learning models with pickle or joblib before using them in an API. This makes loading the model fast and safe.
- FastAPI uses Pydantic for automatic data checks and creates easy-to-use docs at /docs with Swagger UI. Flask needs more manual setup but is easier for beginners.
- Use pip to set up Python environments, install tools like Uvicorn (for FastAPI), scikit-learn, pandas, and joblib. List all dependencies in a requirements.txt file or Dockerfile.
- You can quickly deploy APIs to platforms like Heroku or AWS by using Docker containers. Both support large numbers of users from anywhere in the world.
Preparing the Machine Learning Model

To get your machine learning model ready, you need to save it. You can use tools like pickle or joblib for this task. This way, your model stays safe and sound for later use… and who wouldn’t want that?
Serialize the trained model using pickle or joblib
You need a saved model file for serving. Serialization lets you load the model in an API.
- Pickle saves scikit-learn objects like a Gaussian Naive Bayes trained on the Iris dataset, and you can name the file finalized_model.pkl for easy reference.
- Joblib handles large models and memory mapping, so you get faster load times for big arrays and heavy pandas data.
- Pickle and joblib share security risks, never load files from untrusted sources, and you must treat model files as sensitive in a production environment.
- Use serialization before model serving, so your Flask WSGI app or FastAPI ASGI app can import the model for real-time prediction results.
- You can wrap the loaded model in a RESTful API, and tools like Flask or FastAPI let you expose predict endpoints for clients to call.
- FastAPI uses Pydantic and Starlette, and you can run it with Uvicorn for async throughput, which suits high traffic and model deployment needs.
- Consider BentoML or the bento inference platform for model versioning and ML model serving, they streamline model deployment and integrate with devops pipelines.
- Deploy your API to Heroku, AWS, or Azure, add open telemetry for traces, and link CI/CD so MSFT, other teams, or external clients get stable predictions.
Choosing the Framework: Flask vs. FastAPI
Flask and FastAPI both have their perks. Flask is simple and easy to set up, while FastAPI is faster with better tools for data validation. Each has features that can fit your needs.
Curious about which one suits you best? Read on!
Key differences in performance and features
Here is a quick summary you can scan, before you pick a stack and start coding.
| Area | FastAPI | Flask |
|---|---|---|
| Raw throughput | Over 20,000 requests per second in benchmarks, great for high load. | About 4,000 to 5,000 requests per second, fine for many apps. |
| Async support | Built for asynchronous work, uses ASGI and servers like Uvicorn. | Uses WSGI by default, synchronous patterns are the norm. |
| Validation | Automatic request validation with Pydantic models, fewer runtime errors. | Validation is manual or via extensions, more work for you. |
| Docs and testing | Auto docs via OpenAPI and Swagger UI, handy for testing endpoints. | Manual docs or Flask-RESTful tools, you write more glue code. |
| Project design | Enforces some structure, useful for larger APIs and microservices. | Lets you do what you want, flexible for quick apps and dashboards. |
| Learning curve | Steeper learning curve, but faster setup and better scaling later. | Easier to learn, great for beginners and small projects. |
| Production | Optimized for speed and scaling; common in microservice stacks. | Used widely for web apps; pair with gunicorn or a proxy for scale. |
| Model serving | Pairs well with async I/O, joblib or pickle for model files, and Docker containers. | Works well for dashboards; use joblib or pickle and standard WSGI hosts. |
| Deployment options | Runs on Uvicorn or other ASGI servers; fits AWS, Heroku, container platforms. | Runs on gunicorn or similar; also fits AWS, Heroku, simple PaaS setups. |
Next, you will set up the environment and install the libraries you need.
Setting Up the Environment
To set up your environment, you need to install some libraries. Use pip to get Flask or FastAPI, plus any other tools like Pydantic and Uvicorn for a smooth ride.
Installing necessary libraries and dependencies
You need a clean project environment. You will install core packages and tools.
- Create a virtual environment with python -m venv venv, activate it, and use pip to avoid conflicts; this helps you manage dependencies for model training, model evaluation, and ml deployment.
- Install Flask, Pandas, Pickle (the module), joblib, scikit-learn, CountVectorizer, and MultinomialNB with pip install Flask pandas joblib scikit-learn; these packages let you serve a basemodel, process data, and run text classifiers.
- Add FastAPI, Uvicorn, and Pydantic to requirements when you plan an ASGI app, use pip install fastapi uvicorn pydantic to enable fast async endpoints and clear input validation.
- List every package in requirements.txt, include ML-specific packages like prophet and yfinance if you use them for stock prices, and pin versions so others can reproduce your work.
- Write installation steps in a README and document commands and Python versions, this helps other data science team members and onboarding at Microsoft Corporation or similar firms.
- Use Docker and include all dependencies in the Dockerfile, copy requirements.txt and run pip install -r requirements.txt so the container runs the same code in production.
- Run pip list and pip install –upgrade regularly, you get security patches and new features, and you avoid version drift in artificial intelligence projects.
- Test imports locally, run a small model training and save the model with joblib.dump or pickle.dump, this checks that the API will load the serialized model and serve predictions for setosa, stock prices, or other targets.
- Keep API documentation in mind, include Swagger UIs for FastAPI, use curl commands to verify endpoints, and add notes on model training, self-training, and semi-supervised learning where relevant.
Next, you will build the API with Flask.
Building the API with Flask
Building the API with Flask is pretty easy. You’ll set up routes and handle requests in just a few steps. Think of it like making a tasty sandwich—just layer your code right! Once you have your routes, you can test them using tools like Postman or even curl commands.
Trust me… seeing those responses will feel great!
Creating routes and handling requests
You will create routes in Flask to manage your API. This makes it easy to handle requests from users. Here’s how you can do it:
- Define routes using decorators. Use the
@app.routedecorator to map URLs to functions. - Specify HTTP methods for each route. Use GET, POST, PUT, or DELETE as needed for the function.
- Create a function for each route. This function will process requests and return responses.
- Handle incoming data with Flask’s request object. Access data using
request.jsonorrequest.args. - Send predictions from your model in the response. You can use JSON format to return results.
- Test your routes after coding them. Ensure they respond correctly with tools like Postman or curl.
Building routes is key in creating APIs, especially when managing resources like income and expenses or exposing machine learning predictions!
Testing Flask endpoints
After creating routes and handling requests, it’s time to test your Flask endpoints. This step keeps your API in good shape.
- Use Flask’s built-in test client for easy testing. It helps you send requests without a browser.
- Send HTTP requests like GET or POST to your endpoints. You will check what the API gives back.
- Validate that endpoints respond as expected. Check how they deal with normal and edge-case inputs.
- Log request and response data during tests. This makes it easier to find and fix issues later.
- Test locally before deployment. It saves time and ensures everything works smoothly on your machine.
- Consider using tools like Postman or curl for manual testing too. They offer user-friendly interfaces for checking responses.
- Automated tests can help confirm your API stays reliable. They run checks under various scenarios to ensure stability.
- Make sure that testing improves user trust in your API when it’s live.
- Stay aware of possible bugs or issues by performing regular endpoint checks even after deployment.
- Always be prepared to update your tests as your application grows or changes over time!
Building the API with FastAPI
Building the API with FastAPI is a breeze! You define your input data using Pydantic, and it keeps everything neat. Running the ASGI server with Uvicorn makes your app quick and responsive.
It’s like giving your API a turbo boost, ready to handle requests like a champ!
Defining input data with Pydantic
Pydantic models help define input data for your API in FastAPI. They ensure that the parameters sent by clients have the right types, like floats or integers. You need to use type hints here, so Pydantic can validate them automatically.
This means fewer errors later and a smoother experience when handling requests.
When you get data from users, it comes in as dictionaries. Pydantic makes it easy to access this data and pass it directly to your model’s predict function. If something is wrong with what the client sends, FastAPI catches those mistakes before moving on to processing, which saves time and headache (trust me on that!).
Validating this way creates clearer APIs that are simple yet powerful — perfect for machine learning tasks!
Running the ASGI server with Uvicorn
Now that you’ve defined input data with Pydantic, it’s time to run the ASGI server using Uvicorn. This step is key for serving your FastAPI application smoothly.
- Uvicorn acts as a fast ASGI server implementation. It uses uvloop and httptools, which help handle requests quickly.
- Start Uvicorn by opening your terminal. Type in the command to run your app. You’ll usually write something like
uvicorn main:app --host 0.0.0.0 --port 8008. - The
--hostoption lets your app be accessible from any device on the network, while the--portsets where the server listens for incoming requests. - Uvicorn supports asynchronous operation, making it perfect for handling many requests at once without slowing down.
- Use the RELOAD option while developing; just add
--reloadto your command line statement. This way, any changes you make will automatically restart the server. - Testing how well your API runs is vital; use various tools to send requests to see its responses.
- Uvicorn can serve tens of thousands of requests each second! That’s right—it keeps things running smoothly with low delays.
- Deploying on cloud services, like AWS or Heroku, is simple with Uvicorn too! Just follow their guidelines for setup and get going!
Testing and Validating the API
Testing your API is a key step. You want to make sure it works right, after all! FastAPI comes with Swagger UI. It shows you how to use your endpoints easily. Curl commands let you check if everything runs smoothly too.
So, it’s like having two tools that keep your API in check—nice, right?
Using Swagger UI for FastAPI
FastAPI makes building APIs easier. With Swagger UI, you can see and test your API directly in your browser.
- Swagger UI provides automatic documentation. You get this just by using FastAPI; no extra setup needed.
- The interface is user-friendly. You’ll find it at the /docs endpoint of your FastAPI app.
- It helps you explore API endpoints. You can click around to see what each part does right from your browser.
- Input data requirements are clear. Swagger UI shows you what data to send and how to format it.
- Validation is built-in for parameters. This means if you miss something or mess up the format, Swift API will let you know!
- Response types are also displayed clearly, so you’ll know what to expect after making a request.
- Developers love this feature! It saves time and effort, making testing simple and quick.
- You can use curl commands to verify endpoints too if that’s more your style.
- Debugging becomes easier with all this information in one spot; errors become clearer when documented well.
Accessing Swagger UI is a breeze, making life a lot smoother for both new and seasoned developers alike!
Verifying endpoints with curl commands
Moving on from Swagger UI for FastAPI, you need to verify your API endpoints. Testing is crucial. Here’s how to do it with curl commands:
- Use curl to send requests to your API. This command-line tool helps check if endpoints are working as they should.
- Common HTTP methods include GET, POST, PUT, and DELETE. Each method serves a different purpose in testing your API.
- The -H option sets the content type in the request header. For example, use -H “Content-Type: application/json” when sending JSON data.
- Basic authentication can be done with –user flag in curl commands. This option allows you to send credentials easily.
- For OAuth2 secured APIs, include an access token in the Authorization header of your curl request. It verifies that you have permission to access certain resources.
- Custom headers can be added or changed in your curl commands too. This feature helps test specific conditions required by the API.
- Curl lets you run tests directly from any system without needing a graphical interface. You can quickly validate responses right from the terminal.
- Testing endpoints with curl ensures they work correctly before connecting them to frontend applications. This step is vital for making sure everything operates smoothly.
- Use curl commands to test various scenarios and edge cases with your API endpoints effectively now!
Deploying the API to Production
You can host your API on platforms like Heroku or AWS. These services make it easy to get your model out there for users to access… no fuss, just service!
Hosting on platforms like Heroku or AWS
Hosting your API can make it easy for others to access your machine learning models. Heroku and AWS are two good options for this.
- Heroku provides a simple way to deploy APIs. It works well with Docker, which helps package your app and all its parts.
- To get started on Heroku, you need to create a Dockerfile. This file tells Heroku what your app needs to run.
- Heroku’s Container Registry lets you push your Docker images easily. You just need to log in and use their commands.
- FastAPI runs smoothly on Heroku. It can handle many requests at once without slowing down.
- AWS is great if you want more control over your setup. It offers powerful tools for scaling your app as needed.
- With AWS, you can choose between EC2 or Elastic Beanstalk for hosting your API. Each option has its benefits depending on what you need.
- Both platforms make it easy to access your API from anywhere in the world. This way, other users or IT systems can interact with it.
- A cloud host means no worries about local server issues, too! Your API will be up and running most of the time.
- Consider using Docker Compose if you’re working with multiple services on these platforms; it helps keep things organized.
Next, let’s explore how to test and validate the API like a pro!
Conclusion
You’ve learned how to create APIs for your machine learning models using Flask or FastAPI. Each framework has its strengths. Flask is great for quick projects, while FastAPI shines with speed and features.
You can easily set up environments and build APIs that handle requests smoothly. Using tools like Pydantic helps ensure your data gets validated correctly, saving you from runtime errors.
Deploying your API properly is key to making a real impact in your work. If you’re eager to learn more, explore resources online or community forums where you can deepen your understanding and skills in this area.
Take the leap—your journey into serving machine learning models awaits!
FAQs
1. What is an API to serve machine learning models with Flask or FastAPI?
An API is a web interface that lets apps call your machine learning model. Flask and FastAPI help you make endpoints for model inference. You send JSON in a request, the API runs the model, and it returns JSON. I like FastAPI for async work, Flask for simple apps.
2. How do I deploy a model behind an API?
Save your model as a model file. Load that file in your app. Make an endpoint that accepts input, runs inference, and returns the result. Run the app on a server (ASGI server for FastAPI, WSGI server for Flask). Put the app in a container, like Docker, to ship it.
3. How do I handle performance and scale?
Use async endpoints or background tasks to lower latency. Batch requests when you can, and use a GPU if the model needs it. Run multiple workers and add caching for frequent answers. Monitor throughput and errors, so you can scale when use grows.
4. How do I secure and version my API?
Require authentication and use HTTPS. Check and clean input to avoid bad data. Add rate limits and logging. Put the model version in the endpoint or the model file name, so clients know which model they use. Test and monitor the API and the model outputs (I do this before each release).
References
- https://scikit-learn.org/stable/model_persistence.html
- https://medium.com/data-science/deploy-your-machine-learning-model-as-a-rest-api-4fe96bf8ddcc
- https://www.codecademy.com/article/fastapi-vs-flask-key-differences-performance-and-use-cases
- https://www.contentful.com/blog/fastapi-vs-flask/ (2025-06-12)
- https://medium.com/analytics-vidhya/deploying-your-machine-learning-model-as-a-rest-api-using-flask-c2e6a0b574f5
- https://auth0.com/blog/developing-restful-apis-with-python-and-flask/
- https://medium.com/@anshitvishwa111/hosting-machine-learning-models-as-an-api-service-1cf5cb5a1e2f
- https://www.researchgate.net/publication/335784947_Building_REST_APIs_with_Flask_Create_Python_Web_Services_with_MySQL
- https://medium.com/mlearning-ai/serve-machine-learning-models-with-fastapi-e329ca3a89c6
- https://codesignal.com/learn/courses/working-with-data-models-in-fastapi/lessons/data-modeling-with-pydantic-and-fastapi
- https://noahweber53.medium.com/fastapi-vs-flask-for-ml-deployment-1ca2159befca
- https://testdriven.io/blog/fastapi-machine-learning/ (2023-05-31)
- https://blog.jetbrains.com/pycharm/2024/09/how-to-use-fastapi-for-machine-learning/
- https://www.baeldung.com/curl-rest (2025-03-26)
