RESTful APIs are the backbone of modern web applications. Flask makes it easy to build robust APIs that follow REST principles.
What is REST?
REST (Representational State Transfer) is an architectural style for designing networked applications. RESTful APIs use HTTP methods to perform operations:
- GET: Retrieve data
- POST: Create new resources
- PUT: Update entire resources
- PATCH: Partially update resources
- DELETE: Remove resources
Setting Up Flask for APIs
Start by creating a Flask application:
from flask import Flask, jsonify, request
from flask.views import MethodView
app = Flask(__name__)
# Sample data store
books = [
{'id': 1, 'title': 'Python Guide', 'author': 'John Doe'},
{'id': 2, 'title': 'Flask Tutorial', 'author': 'Jane Smith'}
]
Creating REST Endpoints
GET - Retrieve All Resources
@app.route('/api/books', methods=['GET'])
def get_books():
return jsonify({'books': books})
GET - Retrieve Single Resource
@app.route('/api/books/<int:book_id>', methods=['GET'])
def get_book(book_id):
book = next((b for b in books if b['id'] == book_id), None)
if not book:
return jsonify({'error': 'Book not found'}), 404
return jsonify(book)
POST - Create New Resource
@app.route('/api/books', methods=['POST'])
def create_book():
data = request.get_json()
new_book = {
'id': len(books) + 1,
'title': data['title'],
'author': data['author']
}
books.append(new_book)
return jsonify(new_book), 201
PUT - Update Resource
@app.route('/api/books/<int:book_id>', methods=['PUT'])
def update_book(book_id):
book = next((b for b in books if b['id'] == book_id), None)
if not book:
return jsonify({'error': 'Book not found'}), 404
data = request.get_json()
book.update(data)
return jsonify(book)
DELETE - Remove Resource
@app.route('/api/books/<int:book_id>', methods=['DELETE'])
def delete_book(book_id):
global books
books = [b for b in books if b['id'] != book_id]
return jsonify({'message': 'Book deleted'}), 200
Error Handling
Implement proper error handling:
@app.errorhandler(404)
def not_found(error):
return jsonify({'error': 'Resource not found'}), 404
@app.errorhandler(500)
def internal_error(error):
return jsonify({'error': 'Internal server error'}), 500
Best Practices
- Use proper HTTP status codes
- Validate input data
- Implement pagination for large datasets
- Use API versioning (
/api/v1/books) - Add authentication and authorization
- Document your API (Swagger/OpenAPI)
Conclusion
Building RESTful APIs with Flask is straightforward. Follow REST principles, handle errors properly, and your APIs will be robust and maintainable.