0Pricing
Python Academy · Lesson

Building a REST Endpoint

Return JSON responses.

What is a REST API?

A REST API exposes resources over HTTP using standard verbs. Instead of HTML pages, it returns data, usually as JSON, for other programs to consume.

from flask import Flask, jsonify
app = Flask(__name__)
# REST endpoints return data, not pages
print('REST APIs speak JSON over HTTP')

Returning JSON

jsonify() turns a Python dict or list into a JSON response with the correct content type.

from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/api/status')
def status():
    return jsonify({'status': 'ok'})
print('jsonify builds a JSON response')

All lessons in this course

  1. Routes and Views
  2. Templates with Jinja2
  3. Forms and Request Data
  4. Building a REST Endpoint
← Back to Python Academy