{"id":2002,"date":"2026-07-22T00:22:43","date_gmt":"2026-07-21T17:22:43","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/22\/how-to-build-a-rest-api-with-flask-a-step-by-step-guide\/"},"modified":"2026-07-22T00:22:43","modified_gmt":"2026-07-21T17:22:43","slug":"how-to-build-a-rest-api-with-flask-a-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/22\/how-to-build-a-rest-api-with-flask-a-step-by-step-guide\/","title":{"rendered":"How to Build a REST API with Flask: A Step-by-Step Guide"},"content":{"rendered":"<h1>How to Build a REST API with Flask: A Step-by-Step Guide<\/h1>\n<p>Building a REST API with Flask is straightforward thanks to its lightweight design and Python simplicity. This tutorial will walk you through creating a basic API for managing items, covering setup, routes, and data handling. You&#8217;ll need Python and pip installed.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/via.placeholder.com\/800x600\/4a90d9\/ffffff?text=how%20to%20build%20a%20REST%20API%20with%20Flask\" alt=\"Article illustration\" style=\"display:block;margin:20px auto;max-width:100%;height:auto;border-radius:8px;\" \/><\/p>\n<h2>Setting Up Flask<\/h2>\n<p>First, create a project folder and set up a virtual environment:<\/p>\n<ul>\n<li><code>mkdir flask-api && cd flask-api<\/code><\/li>\n<li><code>python -m venv venv<\/code><\/li>\n<li>Activate it: <code>venv\\Scripts\\activate<\/code> (Windows) or <code>source venv\/bin\/activate<\/code> (Mac\/Linux)<\/li>\n<li>Install Flask: <code>pip install Flask<\/code><\/li>\n<\/ul>\n<p>Now create a file named <code>app.py<\/code> and import Flask.<\/p>\n<h2>Defining Routes and Data<\/h2>\n<p>Start with a simple in-memory data store (a list of dictionaries). Define your first route for GET requests:<\/p>\n<pre><code>from flask import Flask, jsonify, request\napp = Flask(__name__)\n\nitems = [{\"id\": 1, \"name\": \"Item 1\"}, {\"id\": 2, \"name\": \"Item 2\"}]\n\n@app.route('\/items', methods=['GET'])\ndef get_items():\n    return jsonify(items)<\/code><\/pre>\n<h2>Handling POST, PUT, and DELETE<\/h2>\n<p>Add endpoints to create, update, and delete items. Use <code>request.json<\/code> to parse incoming data:<\/p>\n<ul>\n<li><strong>POST \/items<\/strong>: Add a new item with a generated ID.<\/li>\n<li><strong>PUT \/items\/&lt;id&gt;<\/strong>: Update the name of an existing item.<\/li>\n<li><strong>DELETE \/items\/&lt;id&gt;<\/strong>: Remove an item by ID.<\/li>\n<\/ul>\n<p>Here&#8217;s a sample POST handler:<\/p>\n<pre><code>@app.route('\/items', methods=['POST'])\ndef create_item():\n    data = request.json\n    new_id = max(item[\"id\"] for item in items) + 1\n    items.append({\"id\": new_id, \"name\": data[\"name\"]})\n    return jsonify(items[-1]), 201<\/code><\/pre>\n<h2>Testing the API<\/h2>\n<p>Run your app with <code>python app.py<\/code> (by default on port 5000). Test using curl or Postman:<\/p>\n<ul>\n<li><code>curl http:\/\/127.0.0.1:5000\/items<\/code><\/li>\n<li><code>curl -X POST -H \"Content-Type: application\/json\" -d '{\"name\":\"New Item\"}' http:\/\/127.0.0.1:5000\/items<\/code><\/li>\n<\/ul>\n<p>You&#8217;ll see JSON responses. For production, add error handling, validation, and a database.<\/p>\n<h2>Conclusion<\/h2>\n<p>This gives you a working REST API with Flask in minutes. Extend it by adding SQLAlchemy for persistence, Flask-RESTful for structured endpoints, or authentication. Flask&#8217;s flexibility makes it ideal for both quick prototypes and scalable services.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Build a REST API with Flask: A Step-by-Step Guide Building a REST API with Flask is straightforward thanks to its lightweight design and Python simplicity. This tutorial will walk you through creating a basic API for managing items, covering setup, routes, and data handling. You&#8217;ll need Python and pip installed. Setting Up Flask &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-2002","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2002","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=2002"}],"version-history":[{"count":0,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2002\/revisions"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=2002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=2002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=2002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}