{"id":2379,"date":"2026-07-24T00:42:01","date_gmt":"2026-07-23T17:42:01","guid":{"rendered":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/24\/how-to-build-a-rest-api-with-flask-a-step-by-step-guide-2\/"},"modified":"2026-07-24T00:42:01","modified_gmt":"2026-07-23T17:42:01","slug":"how-to-build-a-rest-api-with-flask-a-step-by-step-guide-2","status":"publish","type":"post","link":"https:\/\/sumberlaba.com\/index.php\/2026\/07\/24\/how-to-build-a-rest-api-with-flask-a-step-by-step-guide-2\/","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>Flask is a lightweight Python framework that makes building REST APIs fast and straightforward. In this tutorial, you\u2019ll learn how to create a simple RESTful API with Flask, covering project setup, endpoint creation, request handling, and testing. By the end, you\u2019ll have a functional API you can extend for your own projects.<\/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 virtual environment and install Flask:<\/p>\n<ul>\n<li><code>python -m venv venv<\/code><\/li>\n<li><code>source venv\/bin\/activate<\/code> (or <code>venv\\Scripts\\activate<\/code> on Windows)<\/li>\n<li><code>pip install flask<\/code><\/li>\n<\/ul>\n<p>Create a file named <code>app.py<\/code> and import Flask:<\/p>\n<pre><code>from flask import Flask, request, jsonify\napp = Flask(__name__)\n<\/code><\/pre>\n<h2>Defining Routes and Endpoints<\/h2>\n<p>Use Flask decorators to map HTTP methods to functions. For a simple CRUD API for a resource called &#8220;items&#8221;:<\/p>\n<ul>\n<li><code>GET \/items<\/code> \u2013 return all items<\/li>\n<li><code>POST \/items<\/code> \u2013 create a new item<\/li>\n<li><code>GET \/items\/&lt;id&gt;<\/code> \u2013 get a single item<\/li>\n<li><code>PUT \/items\/&lt;id&gt;<\/code> \u2013 update an item<\/li>\n<li><code>DELETE \/items\/&lt;id&gt;<\/code> \u2013 delete an item<\/li>\n<\/ul>\n<p>Example route:<\/p>\n<pre><code>items = []\n@app.route('\/items', methods=['GET'])\ndef get_items():\n    return jsonify(items)\n<\/code><\/pre>\n<h2>Handling Requests and Responses<\/h2>\n<p>Access request data with <code>request.get_json()<\/code> for POST\/PUT. Return JSON using <code>jsonify()<\/code>. Include proper HTTP status codes:<\/p>\n<pre><code>@app.route('\/items', methods=['POST'])\ndef create_item():\n    data = request.get_json()\n    new_item = {\"id\": len(items)+1, \"name\": data[\"name\"]}\n    items.append(new_item)\n    return jsonify(new_item), 201\n<\/code><\/pre>\n<h2>Testing Your API<\/h2>\n<p>Run the app with <code>flask run<\/code>. Test using <code>curl<\/code> or tools like 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\":\"Book\"}' http:\/\/127.0.0.1:5000\/items<\/code><\/li>\n<\/ul>\n<p>Flask\u2019s built-in server is fine for development. For production, use a WSGI server like Gunicorn.<\/p>\n<p><strong>Conclusion:<\/strong> Flask\u2019s simplicity lets you build REST APIs quickly. Start with the basic structure above, then add authentication, database integration, and error handling as needed. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Build a REST API with Flask: A Step-by-Step Guide Flask is a lightweight Python framework that makes building REST APIs fast and straightforward. In this tutorial, you\u2019ll learn how to create a simple RESTful API with Flask, covering project setup, endpoint creation, request handling, and testing. By the end, you\u2019ll have a functional &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-2379","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2379","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=2379"}],"version-history":[{"count":0,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/posts\/2379\/revisions"}],"wp:attachment":[{"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/media?parent=2379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/categories?post=2379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumberlaba.com\/index.php\/wp-json\/wp\/v2\/tags?post=2379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}