Brian Dantonio | 09 Jul, 2025

How to Build a Simple API: A Friendly Guide

So, you want to build an API.

Maybe you're dreaming of a mobile app that serves daily quotes, or a web dashboard that needs to talk to your database. Or maybe you're just curious. Either way, it's a good instinct. APIs (Application Programming Interfaces) are the invisible bridges that connect apps, servers, and users. Let’s build one together. Slowly, clearly. No prior wizardry required.

Step 1: What is a REST API, Really?

I'm not a fan of jargon, so I'll do my best to explain this in a way anyone can understand even if you're still learning Python.

REST (Representational State Transfer) is an architectural style, and a REST API is a web API that follows its principles.

Here’s the real-world analogy:

Imagine you’re at a coffee shop. You’re the customer (the client), and you place an order with the barista (the API). The barista doesn’t make the coffee themselves; they go fetch it from the kitchen (the server) and bring it back to you. Simple.

That’s what a REST API does. You (or your app) make a request, usually using a URL like /quote/today, and the server responds with data, typically in JSON, which is like neatly packaged information anyone can read.

REST APIs are built on top of HTTP, the same protocol you use to browse the web. They rely on basic actions:

  • GET to retrieve data
  • POST to send new data
  • PUT to update an entire resource
  • PATCH when you're making a partial update.
  • DELETE to remove it

That’s it. No magic. No mysticism. Just structured requests and structured responses, sent over the good old internet.

Step 2: Set Up a Simple Web Server (In Just a Few Lines!)

Python has a wonderful little tool called Flask. It's like a minimalist toolkit for building web servers. To get started:

First, install Flask:

Open your terminal and run:

pip install flask

 

Now, create a file called app.py and type this:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, world!"

if __name__ == '__main__':
    app.run(debug=True)

Then, run it. 

Go to http://127.0.0.1:5000/ in your browser. You should see: Hello, world!

Boom. You’ve just written a functioning web server. Yes, really.

Step 3: Define an API Endpoint

Now let’s define a real API route. In this example, we're building one that delivers a new quote each day.

Add this to your app.py:

@app.route('/quote/today')
def quote_of_the_day():
    return "The journey of a thousand miles begins with a single step."

Step 4: Return Data in JSON (The Universal Language of APIs)

Plain text is nice, but serious apps speak JSON (JavaScript Object Notation). It's structured, language-agnostic, and easily digestible by web and mobile apps alike. Note that we have a good guide for Python JSON if you want to learn more.

Modify your endpoint like so:

 

from flask import jsonify

@app.route('/quote/today')
def quote_of_the_day():
    return jsonify({
        "quote": "The journey of a thousand miles begins with a single step.",
        "author": "Lao Tzu"
    })

 

Now when you visit the endpoint, you’ll get structured data:

 

{
  "quote": "The journey of a thousand miles begins with a single step.",
  "author": "Lao Tzu"
}

Your future app (be it in JavaScript, Swift, or Kotlin) will be able to read this easily.

Step 5: Your API is Now a Backend

That’s it. You’ve now created a backend service. Want to build a mobile app? It can fetch quotes from this API. Want to make a React frontend? It can pull data from here too.

Here’s what your complete app.py might look like now:

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, world!"

@app.route('/quote/today')
def quote_of_the_day():
    return jsonify({
        "quote": "The journey of a thousand miles begins with a single step.",
        "author": "Lao Tzu"
    })

if __name__ == '__main__':
    app.run(debug=True)

Just remember that debug=True is only for testing. If you're pushing this thing live you'll want to remove it.

Final Thoughts

You’ve just taken your first steps into the world of APIs. It's a world that quietly powers almost everything you interact with online. Keep tinkering. Add more routes (/quote/random, /quote/<id>), connect it to a database, or deploy it online.

Remember: Simplicity scales. Start small, but design clearly. It's not a difficult task, building an API. It just might seem like it to someone who's never done it.

 Looking for something more challenging? Check out some of our more advanced Python projects

By Brian Dantonio

Brian Dantonio (he/him) is a news reporter covering tech, accounting, and finance. His work has appeared on hackr.io, Spreadsheet Point, and elsewhere.

View all post by the author

Disclosure: Hackr.io is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission.

Learn More