{"id":2003,"date":"2022-06-28T12:00:00","date_gmt":"2022-06-28T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/how-to-create-an-api-in-python-with-flask-step-by-step\/"},"modified":"2026-08-28T14:15:35","modified_gmt":"2026-08-28T12:15:35","slug":"how-to-create-an-api-in-python-with-flask-step-by-step","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-create-an-api-in-python-with-flask-step-by-step\/","title":{"rendered":"How to Create an API in Python with Flask \u2013 Step by Step"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><em>Learn how to create an API in Python with Flask step by step, using SQLite to build GET, POST, PUT, and DELETE endpoints for student data.<\/em><\/p>\n\n\n<h3>TL;DR<\/h3>\n<ul>\n<li>\n<p>Create a Student class to represent student data.<\/p>\n<\/li>\n<li>\n<p>Connect the Flask API to a SQLite database.<\/p>\n<\/li>\n<li>\n<p>Build GET endpoints to retrieve all students or a student by ID.<\/p>\n<\/li>\n<li>\n<p>Add POST and PUT endpoints to create and update student records.<\/p>\n<\/li>\n<li>\n<p>Add a DELETE endpoint to remove student records from the database.<\/p>\n<\/li>\n<\/ul>\n<p>In this tutorial, you will learn how to create an SDK from the scratch. We would build an API using Flask. This API would retrieve student data from a SQLite database and allow performing other operations such as PUT, POST and DELETE. Next, we would create an SDK that would perform the same operations.<\/p>\n<p>At the end of this tutorial, you will understand the different between an SDK and an API.<\/p>\n<ol>\n<li><a href=\"#t1\">Create the Student Class<\/a><\/li>\n<li><a href=\"#t2\">Setup Your SQLite Database Connection<\/a><\/li>\n<li><a href=\"#t3\">Make a Get Request<\/a><\/li>\n<li><a href=\"#t4\">Get Record By ID<\/a><\/li>\n<li><a href=\"#t5\">POST Request<\/a><\/li>\n<li><a href=\"#t6\">Update Request<\/a><\/li>\n<li><a href=\"#t7\">DELETE Request<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong>\u00a0Start a Server<\/strong><\/p>\n<p>This is the main function, that starts the server<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">if<\/span> __name__ <span style=\"color: #333333;\">==<\/span> <span style=\"background-color: #fff0f0;\">'__main__'<\/span>:\n    app<span style=\"color: #333333;\">.<\/span>run(port<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">8888<\/span>)\n<\/pre>\n<p><strong>Note<\/strong>: This has to be at the end of the file below all other functions in this file.<\/p>\n<h4><strong id=\"t1\">1. Create the Student Class<\/strong><\/h4>\n<p>We would create a class the represents student details. This would be used to create Student object which we would store in our database.<\/p>\n<p>The Student class is given below. Here we are using generating a UUID for each new student object create.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">uuid<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Student<\/span>:\n\n    <span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">__init__<\/span>(<span style=\"color: #007020;\">self<\/span>, firstname, lastname, department):\n        <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>id <span style=\"color: #333333;\">=<\/span> uuid<span style=\"color: #333333;\">.<\/span>uuid4()<span style=\"color: #333333;\">.<\/span>hex\n        <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>firstname <span style=\"color: #333333;\">=<\/span> firstname\n        <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>lastname <span style=\"color: #333333;\">=<\/span> lastname\n        <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>department <span style=\"color: #333333;\">=<\/span> department\n\n    <span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">__str__<\/span>(<span style=\"color: #007020;\">self<\/span>):\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> f<span style=\"background-color: #fff0f0;\">'id:{self.id} '<\/span> \\\n               f<span style=\"background-color: #fff0f0;\">'firstname: {self.firstname}; '<\/span> \\\n               f<span style=\"background-color: #fff0f0;\">'Lastname: {self.lastname}; '<\/span> \\\n               f<span style=\"background-color: #fff0f0;\">'Department: {self.department}'<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Setup Your SQLite Database Connection<\/strong><\/h4>\n<p>You need to simply import the sqlite3 and Flask module. You may also need to install Flask using pip<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">sqlite3<\/span>\n<span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">flask<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #333333;\">*<\/span>\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">json<\/span>\n<span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">student<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> Student\n\napp <span style=\"color: #333333;\">=<\/span> Flask(__name__)\n\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">go_home<\/span>():\n    c <span style=\"color: #333333;\">=<\/span> sqlite3<span style=\"color: #333333;\">.<\/span>connect(<span style=\"background-color: #fff0f0;\">\"student.db\"<\/span>)<span style=\"color: #333333;\">.<\/span>cursor()\n    c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">\"CREATE TABLE IF NOT EXISTS STUDENTS(\"<\/span>\n              <span style=\"background-color: #fff0f0;\">\"id TEXT, firstname TEXT, lastname TEXT, department TEXT)\"<\/span>\n              )\n    c<span style=\"color: #333333;\">.<\/span>connection<span style=\"color: #333333;\">.<\/span>close()\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The function below returns a string for the \u2018\/\u2019 route<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@app<\/span><span style=\"color: #333333;\">.<\/span>route(<span style=\"background-color: #fff0f0;\">'\/'<\/span>, methods<span style=\"color: #333333;\">=<\/span>[<span style=\"background-color: #fff0f0;\">'GET'<\/span>])\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">go_home<\/span>():\n    go_home()\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">'Welcome to the Students API!'<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Make a Get Request<\/strong><\/h4>\n<p>We write a function to return a list of all records in the Students table.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@app<\/span><span style=\"color: #333333;\">.<\/span>route(<span style=\"background-color: #fff0f0;\">'\/getStudents'<\/span>, methods<span style=\"color: #333333;\">=<\/span>[<span style=\"background-color: #fff0f0;\">'GET'<\/span>])\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">get_students<\/span>():\n    c <span style=\"color: #333333;\">=<\/span> sqlite3<span style=\"color: #333333;\">.<\/span>connect(<span style=\"background-color: #fff0f0;\">\"student.db\"<\/span>)<span style=\"color: #333333;\">.<\/span>cursor()\n    c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">\"SELECT * FROM STUDENTS\"<\/span>)\n    data <span style=\"color: #333333;\">=<\/span> c<span style=\"color: #333333;\">.<\/span>fetchall()\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> jsonify(data)\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Get Record By ID<\/strong><\/h4>\n<p>The code below takes an student_id and returns the student\u2019s record<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@app<\/span><span style=\"color: #333333;\">.<\/span>route(<span style=\"background-color: #fff0f0;\">'\/getStudentById\/&lt;student_id&gt;'<\/span>, methods<span style=\"color: #333333;\">=<\/span>[<span style=\"background-color: #fff0f0;\">'GET'<\/span>])\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">get_student_by_id<\/span>(student_id):\n    c <span style=\"color: #333333;\">=<\/span> sqlite3<span style=\"color: #333333;\">.<\/span>connect(<span style=\"background-color: #fff0f0;\">\"student.db\"<\/span>)<span style=\"color: #333333;\">.<\/span>cursor()\n    c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">\"SELECT * FROM STUDENTS WHERE id=?\"<\/span>, (student_id,))\n    data <span style=\"color: #333333;\">=<\/span> c<span style=\"color: #333333;\">.<\/span>fetchone()\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> json<span style=\"color: #333333;\">.<\/span>dumps(data)\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. POST Request<\/strong><\/h4>\n<p>To add a new student record to the database<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@app<\/span><span style=\"color: #333333;\">.<\/span>route(<span style=\"background-color: #fff0f0;\">'\/addStudent'<\/span>, methods<span style=\"color: #333333;\">=<\/span>[<span style=\"background-color: #fff0f0;\">'POST'<\/span>, <span style=\"background-color: #fff0f0;\">'GET'<\/span>])\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">add_student<\/span>():\n    db <span style=\"color: #333333;\">=<\/span> sqlite3<span style=\"color: #333333;\">.<\/span>connect(<span style=\"background-color: #fff0f0;\">\"student.db\"<\/span>)\n    c <span style=\"color: #333333;\">=<\/span> db<span style=\"color: #333333;\">.<\/span>cursor()\n    student <span style=\"color: #333333;\">=<\/span> Student(request<span style=\"color: #333333;\">.<\/span>form[<span style=\"background-color: #fff0f0;\">\"firstname\"<\/span>],\n                      request<span style=\"color: #333333;\">.<\/span>form[<span style=\"background-color: #fff0f0;\">\"lastname\"<\/span>],\n                      request<span style=\"color: #333333;\">.<\/span>form[<span style=\"background-color: #fff0f0;\">\"department\"<\/span>]\n                      )\n    <span style=\"color: #007020;\">print<\/span>(student)\n    c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">\"INSERT INTO STUDENTS VALUES(?,?,?,?)\"<\/span>,\n              (student<span style=\"color: #333333;\">.<\/span>id, student<span style=\"color: #333333;\">.<\/span>firstname, student<span style=\"color: #333333;\">.<\/span>lastname, student<span style=\"color: #333333;\">.<\/span>department))\n    db<span style=\"color: #333333;\">.<\/span>commit()\n    data <span style=\"color: #333333;\">=<\/span> c<span style=\"color: #333333;\">.<\/span>lastrowid\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> json<span style=\"color: #333333;\">.<\/span>dumps(data)\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Update Request<\/strong><\/h4>\n<p>To update a student record we pass the student_id in a url and pass the student object in the request body. The request must be sent as <strong><em>x-www-form-urlencoded<\/em><\/strong>.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@app<\/span><span style=\"color: #333333;\">.<\/span>route(<span style=\"background-color: #fff0f0;\">'\/updateStudent\/&lt;student_id&gt;'<\/span>, methods<span style=\"color: #333333;\">=<\/span>[<span style=\"background-color: #fff0f0;\">'PUT'<\/span>])\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">update_student<\/span>(student_id):\n    db <span style=\"color: #333333;\">=<\/span> sqlite3<span style=\"color: #333333;\">.<\/span>connect(<span style=\"background-color: #fff0f0;\">\"student.db\"<\/span>)\n    c <span style=\"color: #333333;\">=<\/span> db<span style=\"color: #333333;\">.<\/span>cursor()\n    student <span style=\"color: #333333;\">=<\/span> Student(request<span style=\"color: #333333;\">.<\/span>form[<span style=\"background-color: #fff0f0;\">\"firstname\"<\/span>],\n                      request<span style=\"color: #333333;\">.<\/span>form[<span style=\"background-color: #fff0f0;\">\"lastname\"<\/span>],\n                      request<span style=\"color: #333333;\">.<\/span>form[<span style=\"background-color: #fff0f0;\">\"department\"<\/span>]\n                      )\n    <span style=\"color: #007020;\">print<\/span>(student)\n    c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">\"UPDATE STUDENTS SET firstname = ?, lastname =?, department =? WHERE id=?\"<\/span>,\n              (student<span style=\"color: #333333;\">.<\/span>firstname, student<span style=\"color: #333333;\">.<\/span>lastname, student<span style=\"color: #333333;\">.<\/span>department, student_id))\n    db<span style=\"color: #333333;\">.<\/span>commit()\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> json<span style=\"color: #333333;\">.<\/span>dumps(<span style=\"background-color: #fff0f0;\">\"Record was successfully updated\"<\/span>)\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t7\">7. DELETE Request<\/strong><\/h4>\n<p>The function below deletes\u00a0 a student record base on the Id in the url.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@app<\/span><span style=\"color: #333333;\">.<\/span>route(<span style=\"background-color: #fff0f0;\">'\/deleteStudent\/&lt;student_id&gt;'<\/span>, methods<span style=\"color: #333333;\">=<\/span>[<span style=\"background-color: #fff0f0;\">'DELETE'<\/span>])\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">delete_student<\/span>(student_id):\n    db <span style=\"color: #333333;\">=<\/span> sqlite3<span style=\"color: #333333;\">.<\/span>connect(<span style=\"background-color: #fff0f0;\">\"student.db\"<\/span>)\n    c <span style=\"color: #333333;\">=<\/span> db<span style=\"color: #333333;\">.<\/span>cursor()\n    c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">\"DELETE FROM STUDENTS WHERE id=?\"<\/span>, (student_id,))\n    db<span style=\"color: #333333;\">.<\/span>commit()\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> json<span style=\"color: #333333;\">.<\/span>dumps(<span style=\"background-color: #fff0f0;\">\"Record was successfully deleted\"<\/span>)\n<\/pre>\n<p>&nbsp;<\/p>\n<p>I hope you followed and did the procedure successfully. For more, please subscribe to <a href=\"https:\/\/www.youtube.com\/c\/KindsonTheTechPro\" target=\"_blank\" rel=\"noopener\">my YouTube Channel.<\/a><\/p>\n<h3>FAQs<\/h3>\n<p><strong>How do I create an API in Python?<\/strong><\/p>\n<p>You can create an API in Python using Flask by defining routes that handle HTTP requests and connect to your application&#8217;s data or database.<\/p>\n<p><strong>What is Flask used for in this tutorial?<\/strong><\/p>\n<p>Flask is used to create the REST API and define endpoints for performing operations on student records.<\/p>\n<p><strong>How does the API connect to the SQLite database?<\/strong><\/p>\n<p>The tutorial uses Python&#8217;s <code>sqlite3<\/code> module to connect to the SQLite database and execute SQL queries for retrieving and modifying student records.<\/p>\n<p><strong>What HTTP methods are implemented in the Python API?<\/strong><\/p>\n<p>The API implements GET, POST, PUT, and DELETE methods for retrieving, creating, updating, and deleting student records.<\/p>\n<p><strong>What is the difference between an API and an SDK?<\/strong><\/p>\n<p>An API provides endpoints that applications can communicate with, while an SDK provides reusable functions and tools that developers can use to interact with or build applications.<\/p>\n<h3>Final Thoughts<\/h3>\n<p>Creating an API in Python with Flask is a practical way to understand how REST APIs work. This tutorial demonstrates the basic operations needed to manage student data, from retrieving records to creating, updating, and deleting them.<\/p>\n<p>The project also provides a foundation for the next step: building an SDK that performs the same operations. Together, the API and SDK tutorials help demonstrate the difference between exposing functionality through an API and providing reusable development tools through an SDK.<\/p>","protected":false},"excerpt":{"rendered":"<p>Learn how to create an API in Python with Flask step by step, using SQLite to build GET, POST, PUT, and DELETE endpoints for student data.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[7],"tags":[],"class_list":["post-2003","post","type-post","status-publish","format-standard","hentry","category-python-tutorials"],"acf":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=2003"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2003\/revisions"}],"predecessor-version":[{"id":2566,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2003\/revisions\/2566"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=2003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=2003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=2003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}