₦airaland Forum

Welcome, Guest: Register • Login • With Google • Trending • Recent • New

Stats: 3,344,229 members, 8,510,793 topics. Date: Saturday, 31 October 2026 at 05:55 AM

Toggle theme

HOWTO: Create A Simple API With Python And Flask - Programming - Nairaland

Nairaland Forum › Science/Technology › Programming › HOWTO: Create A Simple API With Python And Flask (1809 Views)

1 Reply

HOWTO: Create A Simple API With Python And Flask by pystar(op): 4:51am On Aug 29, 2014
This is a simple flask app: Pardon the lack of indentation in my code.
Installing Flask-Restful in your flask app makes building API's extremely simple.

$ PIP install flask-restful

######Simple flask app with API to expose the app##########

from flask import Flask
from flask.ext.restful import Resource, API

app = Flask(__name__)
api = Api(app)

@app.route("/" )
@app.route("/index" )
@app.route("/home" )
def index():
return "Returning data"

###This exposes your app as an API###

class Nairaland(Resource):
def get(self):
return {"data" : "Returning data"}

app.add_resource(Nairaland, "/api/v1.0/" )

if __name__ == "__main__":
app.run()
Re: HOWTO: Create A Simple API With Python And Flask by pystar(op): 4:56am On Aug 29, 2014
You can copy the above code into a file and name it api.py, kindly indent it to enable it run. Ensure you have installed the flask-restful extension into your flask installation (hopefully you should be using virtualenv).

Run the app with
$ python api.py and visit http://localhost:5000/api/v1.0 to see the api in action.

To view the API returned data programmatically
$ curl -i http://localhost:5000/api/v1.0

In my next post, I will show how to post data with the API. (Off to work grin)
1 Reply

Build A Bare Bones Reddit Clone With Python And Flask (hands On Tutorial Thread) • Pls Help With Python Problem • Dll Load Failure With Py2exe (python And Qt) • 2 • 3 • 4

Please HELP Me With This Java Program • Techgeek12 Is A Scammer And A Thief. You All Be Warned. • A 19 Year Old Me Gave An Advice That Doesn't Help Me 3 Years Later