Creating a personal API of your data projects with Fastapi

Have you had a dirty jokyter brochure attached to the attached code to use other anti-data data? That you are actively or work, if you point to the most, you may have responded to something like “so many way”.
You are not alone.
Maybe try to share information with your partner or connect your latest ML model to the slick dashboard, but to send CSVs or rebuild the dashboard from the beginning does not feel right.
Here's today's maintenance (topic): Create a personal API.
In this post, I'll show you how you can set simple, Fastapi Fastapi's power to express your details or models and finally Provide your data projects according to the status they deserve.
Even if you are a Solo Solo Solo Live, a student with a separate project, or a thorn engineer, this is for you.
And no, I'm not paid to promote this service. It may be good, but the truth is far from it. I have already enjoyed using it and thought it had to be shared.
Let's review today's content list:
- What is personal API? (And why should you take care?)
- Some use charges
- Setting up with Surapi
- Store
What is personal API? (And why should you take care?)
99% of people who read this will be familiar with the idea of API. But by this 1%, here is a short intro that will be compatible with the code in the following sections:
A War (Organizing Application Interm)) set of rules and tools that allow a variety of software programs to communicate with each other. Explains What to ask the program to makeLike “Give me weather forecast” or “Send a message.” And that program treats the application after the scenes and restores the result.
So, what is a API of a person? In fact, it is a web service that exposes your data or logic systematically, practical. Think about it as a little app answering HTTP requests with JSON for your data types.
Why is that good idea? In my opinion, it has different benefits:
- As mentioned, contest. We can use it from our writing books, Deskboard or texts without rewriting the same code several times.
- Cooperation: Your group players can easily access your data using the IPI endpoints without requiring double your code or download the same information at their equipment.
- Transaction: You can send anywhere – in your area, in the cloud, in the container, or even at Raspberry P.
- Examination: You need to test a new feature or model update? Pourage your api and quickly test all clients (manuals, apps, apps, Dashboards).
- Encoposotion and type: You can change your logic (V1, V1, etc.) and separate raw data from the logic used clean. That's great plus to save.
And Surapi is ready for this. But let's see some of the real use charges where anyone is like you and have benefited from personal API.
Some use charges
Whether you are a data scientist, analyst, ml engineer, or just forming cool things on weekends, a personal API can be your secret manufacturer. Here are three examples:
- Model-as-a-service (Weight): Train the ML model in the area and display it in your community with endpoint like
/predict. Also the options from here are not over: speed rotation, including before … - Dashboard-Ready Data: Serve the deserted dassasets, clean, and filtered in the Bi tools or the dash of the dirty. You can include logic on your API, so the Dashboard remains lightweight and does not resort or integrate.
- A detailed data base: When working on a project that contains multiple correspondence, is it happening to you that the first cells of all contain the same code? Well, what if you combine all that code on your API and receive it in one application? Yes, you can enter this description and call the same job, but creating API allows you to continue one step, to use it easily from anywhere (not only).
I hope you get a point. Options do not end, such as its usefulness.
But let's get to an exciting part: to build API.
Setting up with Surapi
As every time, first set the environment with your favorite tool (venv, PIPEPV …). Then enter Fastapi and Uvicorn with pip install fastapi uvicorn. Let's understand what they do:
- Fastapi[1]: It is a library to allow us to improve API, in fact.
- Vicorn[2]: That's what will allow us to conduct a web server.
Once installed, we need only one file. For convenience, we will call it App.He.
Now let's put the context in what we will do: think of the good irrigation system of our vegetable garden home. The irrigation system is quite simple: We have moisture senses that read soil moisture and then we want to make a plan where less than 30%.
Of course we want to change in your area, so if the limit is first to drop water. But we are interested in accessing the program remotely, maybe read the current value or even causing water pump if we want. This is when personal API can come on useful.
Here is the basic code that will allow us to do just that (notice that I use another library, DuckdB[3]Because that is where I would save information – but you can just use SQLITE3, PANDAS, 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"}
Specific reading, this code separates three main blocks:
- Which to property
- To set an app item and DB connection
- Creating API ENDPOINTS
1 and 2 are upright, so we will focus on the third. What I did here creates 3 endpoints for their jobs:
/last_moistureindicates the final number of senses (recent latest)./moisture_reads/{day}It helps to see the senses read from one day. For example, if I wanted to compare moisture levels in the winter and those summer, I would check what/moisture_reads/2024-01-01and see the difference with/moisture_reads/2024-08-01.
But I have done it and can read to find parameters if I am interested in examining some time. For example:/moisture_reads/2024-01-01?time=10:00/trigger_irrigationHe will do what the word means.
So we only lost one part, starting the server. See how easy it is to use in place:
uvicorn app:app --reload
Now I can visit:
But it doesn't end here. Fastapi provides another conclusion that is found in that Display the default partnership documents of our API. In our case:
It is very helpful when API works together, because we do not have to check the code so that we can see all the ENDPOINS we have access!
And with just a few of the code lines, very few in fact, we were able to build our API. Apparently, it can be very complicated (and maybe right) but that was not today's purpose.
Store
With just a few Python lines and fastapi and fastapi lines, you now realize how easy it is to expose data or mind with personal API. Whether you create a smart irrigation system, displaying a machine study model, or tired of the same re-rewriting of all books – this method brings your projects.
And this is just the beginning. You can:
- Add Reality and Summer
- Serve in a cloud or raspberry pi
- It is a time in the frontind or telegramb bot
- Todify your portfolio to a project for life, breathing
If you ever wanted your data to work feel As a real product – this is your gate.
Let me know if you have built something that is inactive. Or better, send me url your /predict, /last_moistureWhatever in API did. I would like to see if you come from you.
Resources
[1] Ramíreez, S. (2018). Fastapi (Version 0.109.2) [Computer software].
[2] That's some. (2018). Vicorn (Version 0.27.0) [Computer software].
[3] Mühleisen, H., Raasveldt, M., & Duckd Providers. (2019). DuckdB (Version 0.10.2) [Computer software].



