{"id":2102,"date":"2026-07-22T18:07:21","date_gmt":"2026-07-22T11:07:21","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/22\/how-to-build-a-rest-api-with-python-flask\/"},"modified":"2026-07-22T18:07:21","modified_gmt":"2026-07-22T11:07:21","slug":"how-to-build-a-rest-api-with-python-flask","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/22\/how-to-build-a-rest-api-with-python-flask\/","title":{"rendered":"How to Build a REST API with Python Flask"},"content":{"rendered":"<h1>How to Build a REST API with Python Flask<\/h1>\n<p>Flask is a lightweight Python microframework that makes building REST APIs fast and clean. In this tutorial, you\u2019ll create a simple REST API with CRUD operations for managing tasks. By the end, you\u2019ll have a fully functional API that returns JSON responses.<\/p>\n<p>First, set up a virtual environment and install Flask: <code>pip install flask<\/code>. Create a new file <code>app.py<\/code> and import Flask, <code>request<\/code>, and <code>jsonify<\/code>. Then initialize your app with <code>app = Flask(__name__)<\/code>. For data, use a simple in-memory list of dictionaries\u2014good for prototyping.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/via.placeholder.com\/800x600\/4a90d9\/ffffff?text=how%20to%20build%20a%20REST%20API%20with%20Python%20Flask\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>Define Routes and Endpoints<\/h2>\n<p>Use Flask\u2019s <code>@app.route()<\/code> decorator to map HTTP methods to functions. For example:<\/p>\n<ul>\n<li><code>GET \/tasks<\/code> \u2013 returns all tasks.<\/li>\n<li><code>GET \/tasks\/&lt;id&gt;<\/code> \u2013 returns a specific task.<\/li>\n<li><code>POST \/tasks<\/code> \u2013 creates a new task.<\/li>\n<li><code>PUT \/tasks\/&lt;id&gt;<\/code> \u2013 updates an existing task.<\/li>\n<li><code>DELETE \/tasks\/&lt;id&gt;<\/code> \u2013 deletes a task.<\/li>\n<\/ul>\n<p>Implement each function with simple logic. For example, in the POST route, get JSON data via <code>request.get_json()<\/code>, add an ID, and append to your tasks list. Return <code>jsonify(task)<\/code> with status code 201.<\/p>\n<h2>Handle Request and Response<\/h2>\n<p>Use <code>request.method<\/code> to differentiate actions on the same route. Always return JSON using <code>jsonify()<\/code>. For missing resources, return <code>({'error': 'Not found'}, 404)<\/code>. For invalid data, return status 400. This ensures your API follows REST best practices.<\/p>\n<h2>Testing Your API<\/h2>\n<p>Run <code>python app.py<\/code> and use a tool like Postman or curl to test endpoints. Example curl: <code>curl http:\/\/127.0.0.1:5000\/tasks<\/code>. For POST: <code>curl -X POST -H \"Content-Type: application\/json\" -d '{\"title\":\"New Task\"}' http:\/\/127.0.0.1:5000\/tasks<\/code>. Verify responses are correct.<\/p>\n<p>You now have a basic REST API with Flask. For production, add input validation, use a database, and structure your code with Blueprints. Flask makes scaling simple and is ideal for microservices.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Build a REST API with Python Flask Flask is a lightweight Python microframework that makes building REST APIs fast and clean. In this tutorial, you\u2019ll create a simple REST API with CRUD operations for managing tasks. By the end, you\u2019ll have a fully functional API that returns JSON responses. First, set up a &hellip; <\/p>\n","protected":false},"author":2716,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-2102","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/users\/2716"}],"replies":[{"embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/comments?post=2102"}],"version-history":[{"count":0,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2102\/revisions"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=2102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=2102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=2102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}