Skip to content
Sign upLog in
← Back to Community

Making a Simple API with Flask and repl.it db

Profile icon
[deleted]

Making a simple API with Flask and repl.it db

Hi everyone! Today I'll be showing you all a way to make a simple api on repl it. first, let me walk you through the boilerplate code we need:

from flask import Flask, jsonify, request #Imports app = Flask(__name__) #Initiate the app @app.route('/', methods=["GET"]) #Home page def home(): return "<h1>Home for 'insert api name here' api.</h1>" app.run(host="0.0.0.0", port="8080") #run app

Now that we have our boilerplate, we start on the API functions. Add something like this under the home page. This is if you want to make a book api:

@app.route("/api/v1/data/add", methods=["GET"]) def adddata(): title = request.args.get("title") author = request.args.get("author") genre = request.args.get("genre") try: db[title] return "Book has already been added." except: db[title] = title db[title + "a"] = author db[title + "g"] = genre return "Data has been added!"

Now you can add data to your api. To pull that data from it now, add this under it:

@app.route("/api/v1/data/get", methods=["GET"] def getdata(): title = request.args.get("title") try: tvalue = db[title] avalue = db[title + "a"] gvalue = db[title + "g"] data = {} data.update({"title": tvalue}) data.update({"author": avalue}) data.update({"genre": gvalue}) return jsonify(data) except: return "Book has not been added."

And just like that, you've got a barebones api! Comment with a repl link to show off what you do with this.

Voters
Profile icon
ANJANAMENON1
Profile icon
DynamicSquid
Profile icon
Rak2006
Profile icon
btfuss
Profile icon
Kudos
Comments
hotnewtop
Profile icon
DynamicSquid

This is a neat little tutorial!