₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,331,204 members, 8,449,139 topics. Date: Tuesday, 21 July 2026 at 11:06 AM

Toggle theme

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

Nairaland ForumScience/TechnologyProgrammingHOWTO: Create A Simple API With Python And Flask (1789 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 ProblemDll Load Failure With Py2exe (python And Qt)234

Please HELP Me With This Java ProgramDo We Have Competent Programmer From Scratch In Nigeria?Are You In Need Of A Mobile Or Web Enterprise Application?