Lesson 47 of 55
PythonBuilding a Simple REST API
20 minutes
Course progress85%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 47 of 55
Python20 minutes
Keep these ideas in mind as you work through the lesson.
Flask can also serve JSON APIs. Below we expose a /api/users endpoint that returns a list of user objects.
from flask import Flask, jsonify
app = Flask(__name__)
users = [
{'id': 1, 'name': 'Alice'},
{'id': 2, 'name': 'Bob'}
]
@app.route('/api/users')
def get_users():
return jsonify(users)
if __name__ == '__main__':
app.run(debug=True)
Visit http://127.0.0.1:5000/api/users to see the JSON response. You can extend the API with POST, PUT, and DELETE methods for full CRUD support.