Data Science

Use FastAPI to build personal APIs for your data projects

Do you have a cluttered laptop with copy code just to reuse some data brawl logic? Whether you’re for passion or for work, if you code frequently, you may have answered “too many ways.”

you are not alone.

Maybe you’re trying to share data with colleagues or plug the latest ML model into a smooth dashboard, but sending a CSV or rebuilding the dashboard from scratch isn’t right.

Here is the fix (and topic) today: Build a personal API for yourself.
In this post, I will show you how to set up a lightweight, powerful FastAPI service to reveal your dataset or model as well at last Provide the modularity that your data projects should be.

Whether you are a solo data science enthusiast, a student with accompanying projects or an experienced ML engineer, this is for you.

No, I was not paid to promote this service. Very good, but the reality is far from it. I just love using it and I think it’s worth sharing.

Let’s review today’s directory:

  1. What is a personal API? (Why do you care?)
  2. Some use cases
  3. Settings with fastapi
  4. in conclusion

What is a personal API? (Why do you care?)

99% of those who read this book are already familiar with the API concept. But for this 1% of people, here is a brief introduction that will complement the code in the next section:

one API (Application Programming Interface) is a set of rules and tools that allow different software applications to communicate with each other. It defines What can you ask the program to dosuch as “Give me a weather forecast” or “Send a message”. The program handles behind-the-scenes requests and returns results.

So, what is Personal API? It is essentially a small web service that reveals your data or logic in a structured and reusable way. Think of it as a mini application that responds to HTTP requests using the JSON version of the data.

Why is this a good idea? I think it has different advantages:

  • As mentioned earlier, Reusable. We can use it from a notebook, dashboard, or script without having to rewrite the same code multiple times.
  • cooperate: Your teammates can easily access your data through API endpoints without having to copy your code or download the same dataset in their computer.
  • portability: You can deploy anywhere – locally deployed in the cloud, in containers, and even on a Raspberry Pi.
  • test: Need to test for new features or model updates? Push it into your API and test it immediately on all clients (notebooks, applications, dashboards).
  • Packaging and version control: You can version the logic (v1, v2, etc.) and cleanly separate from the processing logic. This is a huge advantage of maintainability.

Fastapi is perfect. But let’s look at some real use cases where people like you and me will benefit from personal APIs.

Some use cases

Whether you’re a data scientist, analyst, ML engineer, or building something cool on weekends, personal APIs can be your secret productivity weapon. Here are three examples:

  • Model as a service (Mass): Train the ML model locally and expose it to your public through such an endpoint /predict. The options to start from here are endless: Quick prototyping, integrating it into the front end…
  • Dashboard ready data: Provide preprocessed, cleaned, and filtered datasets to BI tools or custom dashboards. You can centralize your logic in the API, so the dashboard remains lightweight and does not reimplement filtering or aggregation.
  • Reusable data access layer: Have you ever happened to you all these cells always contain the same code when working from a project that contains multiple notebooks? Well, what if you centralize all your code into the API and complete that code from a single request? Yes, you can modularize it and call a function for the same thing, but creating an API can take you a step further and be able to use it easily from anywhere (not only locally).

Hope you understand this. The options are endless, just like its practicality.

But let’s get into the interesting part: building the API.

Settings with fastapi

As always, first set up the environment using your favorite Env tools (Venv, pipenv…). Then, use pip install fastapi uvicorn. Let’s understand their work:

  • Fastapi[1]: It is the library that will enable us to develop APIs at its core.
  • Uweikang[2]: This will enable us to run a web server.

After installation, we only need one file. For simplicity, we call it app.py.

Now, let’s introduce some context in what we are going to do: Imagine that we are building an intelligent irrigation system for the vegetable garden at home. The irrigation system is very simple: we have a moisture sensor that can read soil moisture at a certain frequency, and when the system is below 30%, we want to activate the system.

Of course, we want to automate it locally, so when it reaches the threshold, it starts dripping. However, we are also interested in being able to access the system remotely, maybe reading the current value, and even triggering the water pump. That’s what personal APIs can use.

Here is the basic code that allows us to do this (note that I’m using another library, DuckdB[3]because that’s where I want to store the data – but you can just use SQLite3, Pandas or whatever you like):



import datetime

from fastapi import FastAPI, Query
import duckdb

app = FastAPI()
conn = duckdb.connect("moisture_data.db")

@app.get("/last_moisture")
def get_last_moisture():
    query = "SELECT * FROM moisture_reads ORDER BY day DESC, time DESC LIMIT 1"
    return conn.execute(query).df().to_dict(orient="records")

@app.get("/moisture_reads/{day}")
def get_moisture_reads(day: datetime.date, time: datetime.time = Query(None)):
    query = "SELECT * FROM moisture_reads WHERE day = ?"
    args = [day]
    if time:
        query += " AND time = ?"
        args.append(time)
    
    return conn.execute(query, args).df().to_dict(orient="records")

@app.get("/trigger_irrigation")
def trigger_irrigation():
    # This is a placeholder for the actual irrigation trigger logic
    # In a real-world scenario, you would integrate with your irrigation system here
    return {"message": "Irrigation triggered"}

Read vertically, this code separates three main blocks:

  1. import
  2. Set up application objects and database connections
  3. Create API endpoints

1 and 2 are very simple, so we focus on the third one. What I’m doing here is creating 3 endpoints with their own functionality:

  • /last_moisture Displays the last sensor value (the most recent sensor value).
  • /moisture_reads/{day} It is useful to see sensors read from the day. For example, if I wanted to compare the moisture level in winter to the moisture level in summer, what would I check /moisture_reads/2024-01-01 and observed with /moisture_reads/2024-08-01.
    However, if I’m interested in checking a specific time, I’m also able to read to get the parameters. For example: /moisture_reads/2024-01-01?time=10:00
  • /trigger_irrigation Can make a name suggestion.

Therefore, we only lack a part of it, start the server. Check out the simplest and simple way to run it locally:

uvicorn app:app --reload

Now I can access:

But that’s not over. FastApi provides another endpoint that displays automated interactive documentation for our API. In our case:

This is very useful when the API collaborates because we don’t need to check the code to see all the endpoints we can access!

And, with only a few lines of code, and in fact, very little, we have been able to build our personal API. Obviously, it may become more complex (probably should), but that’s not what it is today.

in conclusion

With just a few lines of power of Python and Fastapi, you’ve now seen the ease of revealing data or logic through personal APIs. Whether you are building smart irrigation systems, revealing machine learning models, or getting tired of a way to rewrite the same bickering logic on your laptop – this approach brings modularity, collaboration, and scalability to your project.

This is just the beginning. You can:

  • Add authentication and version control
  • Deploy to the cloud or raspberry Pi
  • Link it to the front end or telegram robot
  • Turn your portfolio into a living breathing project center

If you ever wanted your data to work Feel Just like a real product, this is your portal.

Let me know if you will build something cool about it. Even better, send my URL to you /predict,,,,, /last_moistureor any API you make. I really want to see what you came up with.

resource

[1] Ramírez, S. (2018). Fastapi (version 0.109.2) [Computer software].

[2] coding. (2018). Uweikang (version 0.27.0) [Computer software].

[3] Mühleisen, H., Raasveldt, M. and DuckDB contributors. (2019). DuckdB (version 0.10.2) [Computer software].

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button