API
You will learn how an API lets two programs talk to one another.
Senior Developer
What is an API?
Alright, let’s start with the basics. API stands for Application Programming Interface. But let’s not get lost in the jargon. Think of an API as a waiter at a restaurant. You (the client) sit down at a table (your app or website) and look at the menu (the options or services you can access). You tell the waiter (the API) what you want to eat (like fetching data or posting a tweet). The waiter then goes to the kitchen (the server) to tell the chefs (the backend) what you ordered. Once your food is ready, the waiter brings it back to you. That’s basically what an API does!
In the tech world, APIs are the middlemen that let different software applications talk to each other. For example, when you use a weather app, the app is making a request to a weather service API to get the current forecast, which it then shows you.
API Examples
- Facebook API: When you see a "Share on Facebook" button on a website, it's using Facebook's API to let you share content directly from the site to your Facebook feed.
- Twitter API: Ever noticed how some apps let you post tweets without opening the Twitter app? That’s the Twitter API in action, letting other apps interact with Twitter to create posts, read your feed, etc.
- Google Maps API: A lot of websites and apps use Google Maps to show you locations. They’re able to do this because they’re tapping into the Google Maps API, which provides all the map data.
The 7 Main Routes for a CRUD App
CRUD stands for Create, Read, Update, Delete—the basic operations you can do with data in an application. Now, let’s talk about the seven main routes you’ll find in a CRUD app, like a simple blog or a to-do list app:
- Index: This route shows you a list of all items. For example, it could display a list of all blog posts or all tasks in a to-do app. It's like the menu showing all the dishes.
- Show: This route shows you one specific item in detail. Think of it as clicking on a blog post title to read the full article.
- New: This route gives you a blank form to create a new item. It’s like an empty form you fill out when you want to write a new blog post.
- Create: This route takes the data you entered in the "New" form and saves it to the database. When you hit “Submit” on that new blog post, the "Create" route makes it official.
- Edit: This route gives you a form with the existing data so you can update it. Imagine wanting to correct a typo in a blog post—that's where "Edit" comes in.
- Update: This route takes the changes you made in the "Edit" form and saves them to the database. Once you finish editing your post and click “Save,” the "Update" route kicks in.
- Delete: This route removes an item from the database. When you decide to delete a blog post, this is the route that handles it.
In a Nutshell
So, in short, a web API is like the waiter that takes your orders and brings your food from the kitchen to your table. And in the world of web apps, these CRUD routes are the main orders you can place—whether you’re creating a new dish (item), updating an old one, or just browsing the menu (list of items).
Example
This is how an Web API would look for a restaurant app.
/appetizers/chillie-fries
/appetizers/shrimp-platter
/drinks/soda
/drinks/tea
/entres/hamburger
/entres/pizza
/entres/steak
/entres/chicken-sandwich
And this is how the backend code for a pseudo restaurant api may look
from flask import Flask
app = Flask(__name__)
port = 3000
@app.route('/<category>/<type>')
def food(category,type):
return f"Serving some delicious {type} {category}"
if __name__ == '__main__':
print("Server is listening on port " + str(port))
app.run(debug=True,port=port)
Auth Routes
/auth/login GET (Login Form)
/auth/login POST (Login Function)
/auth/register GET (Register Form)
/auth/register POST (Register Function)
from flask import Flask,jsonify
import random
app = Flask(__name__)
port = 3000
users = (
{"id": 1, "username": "Joe", "password": "password"},
{"id": 2, "username": "Sam", "password": "password"},
{"id": 3, "username": "Katie", "password": "password"},
{"id": 4, "username": "Henry", "password": "password"},
{"id": 5, "username": "Barbra", "password": "password"}
)
@app.route('/')
def index():
return "HI"
@app.route('/users')
def all_users():
return jsonify(users)
@app.route('/users/create')
def create_user():
users.append( {"id": random.randint(3, 1000), "username": "Keith", "password": "password"})
return jsonify(users)
@app.route('/users/<id>')
def retrieve_user(id):
user = [d for d in users if d['id'] == int(id)][0]
return jsonify(user)
@app.route('/users/<id>/update/<username>')
def update_user(id,username):
user = [d for d in users if d['id'] == int(id)][0]['username'] = username
list(users).append(user)
return jsonify(users)
# This method is not quite right
@app.route('/users/<id>/delete')
def delete_user(id):
user = [ (a,list(users).remove(d)) for (a,d) in enumerate(users) if d['id'] == int(id)][0]
return jsonify(list(user))
if __name__ == '__main__':
print("Server is listening on port " + str(port))
app.run(debug=True,port=port)