Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
5

Making a Simple API with Flask and repl.it db

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.

2 years ago

Voters

ANJANAMENON1
ANJANAMENON1
DynamicSquid
DynamicSquid
Rak2006
Rak2006
btfuss
btfuss
Kudos
Kudos

Comments

TopNew
1
DynamicSquid
DynamicSquid

This is a neat little tutorial!

8 months ago
Load more