{"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-07-05T03:26:40","modified_gmt":"2026-07-05T01:26:40","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":"<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>:\r\n    app<span style=\"color: #333333;\">.<\/span>run(port<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">8888<\/span>)\r\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>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Student<\/span>:\r\n\r\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):\r\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\r\n        <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>firstname <span style=\"color: #333333;\">=<\/span> firstname\r\n        <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>lastname <span style=\"color: #333333;\">=<\/span> lastname\r\n        <span style=\"color: #007020;\">self<\/span><span style=\"color: #333333;\">.<\/span>department <span style=\"color: #333333;\">=<\/span> department\r\n\r\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>):\r\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> f<span style=\"background-color: #fff0f0;\">'id:{self.id} '<\/span> \\\r\n               f<span style=\"background-color: #fff0f0;\">'firstname: {self.firstname}; '<\/span> \\\r\n               f<span style=\"background-color: #fff0f0;\">'Lastname: {self.lastname}; '<\/span> \\\r\n               f<span style=\"background-color: #fff0f0;\">'Department: {self.department}'<\/span>\r\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>\r\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>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">json<\/span>\r\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\r\n\r\napp <span style=\"color: #333333;\">=<\/span> Flask(__name__)\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">go_home<\/span>():\r\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()\r\n    c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">\"CREATE TABLE IF NOT EXISTS STUDENTS(\"<\/span>\r\n              <span style=\"background-color: #fff0f0;\">\"id TEXT, firstname TEXT, lastname TEXT, department TEXT)\"<\/span>\r\n              )\r\n    c<span style=\"color: #333333;\">.<\/span>connection<span style=\"color: #333333;\">.<\/span>close()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The function below returns a string for the &#8216;\/&#8217; 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>])\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">go_home<\/span>():\r\n    go_home()\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">'Welcome to the Students API!'<\/span>\r\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>])\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">get_students<\/span>():\r\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()\r\n    c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">\"SELECT * FROM STUDENTS\"<\/span>)\r\n    data <span style=\"color: #333333;\">=<\/span> c<span style=\"color: #333333;\">.<\/span>fetchall()\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> jsonify(data)\r\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&#8217;s 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>])\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">get_student_by_id<\/span>(student_id):\r\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()\r\n    c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">\"SELECT * FROM STUDENTS WHERE id=?\"<\/span>, (student_id,))\r\n    data <span style=\"color: #333333;\">=<\/span> c<span style=\"color: #333333;\">.<\/span>fetchone()\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> json<span style=\"color: #333333;\">.<\/span>dumps(data)\r\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>])\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">add_student<\/span>():\r\n    db <span style=\"color: #333333;\">=<\/span> sqlite3<span style=\"color: #333333;\">.<\/span>connect(<span style=\"background-color: #fff0f0;\">\"student.db\"<\/span>)\r\n    c <span style=\"color: #333333;\">=<\/span> db<span style=\"color: #333333;\">.<\/span>cursor()\r\n    student <span style=\"color: #333333;\">=<\/span> Student(request<span style=\"color: #333333;\">.<\/span>form[<span style=\"background-color: #fff0f0;\">\"firstname\"<\/span>],\r\n                      request<span style=\"color: #333333;\">.<\/span>form[<span style=\"background-color: #fff0f0;\">\"lastname\"<\/span>],\r\n                      request<span style=\"color: #333333;\">.<\/span>form[<span style=\"background-color: #fff0f0;\">\"department\"<\/span>]\r\n                      )\r\n    <span style=\"color: #007020;\">print<\/span>(student)\r\n    c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">\"INSERT INTO STUDENTS VALUES(?,?,?,?)\"<\/span>,\r\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))\r\n    db<span style=\"color: #333333;\">.<\/span>commit()\r\n    data <span style=\"color: #333333;\">=<\/span> c<span style=\"color: #333333;\">.<\/span>lastrowid\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> json<span style=\"color: #333333;\">.<\/span>dumps(data)\r\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>])\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">update_student<\/span>(student_id):\r\n    db <span style=\"color: #333333;\">=<\/span> sqlite3<span style=\"color: #333333;\">.<\/span>connect(<span style=\"background-color: #fff0f0;\">\"student.db\"<\/span>)\r\n    c <span style=\"color: #333333;\">=<\/span> db<span style=\"color: #333333;\">.<\/span>cursor()\r\n    student <span style=\"color: #333333;\">=<\/span> Student(request<span style=\"color: #333333;\">.<\/span>form[<span style=\"background-color: #fff0f0;\">\"firstname\"<\/span>],\r\n                      request<span style=\"color: #333333;\">.<\/span>form[<span style=\"background-color: #fff0f0;\">\"lastname\"<\/span>],\r\n                      request<span style=\"color: #333333;\">.<\/span>form[<span style=\"background-color: #fff0f0;\">\"department\"<\/span>]\r\n                      )\r\n    <span style=\"color: #007020;\">print<\/span>(student)\r\n    c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">\"UPDATE STUDENTS SET firstname = ?, lastname =?, department =? WHERE id=?\"<\/span>,\r\n              (student<span style=\"color: #333333;\">.<\/span>firstname, student<span style=\"color: #333333;\">.<\/span>lastname, student<span style=\"color: #333333;\">.<\/span>department, student_id))\r\n    db<span style=\"color: #333333;\">.<\/span>commit()\r\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>)\r\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>])\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">delete_student<\/span>(student_id):\r\n    db <span style=\"color: #333333;\">=<\/span> sqlite3<span style=\"color: #333333;\">.<\/span>connect(<span style=\"background-color: #fff0f0;\">\"student.db\"<\/span>)\r\n    c <span style=\"color: #333333;\">=<\/span> db<span style=\"color: #333333;\">.<\/span>cursor()\r\n    c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">\"DELETE FROM STUDENTS WHERE id=?\"<\/span>, (student_id,))\r\n    db<span style=\"color: #333333;\">.<\/span>commit()\r\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>)\r\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","protected":false},"excerpt":{"rendered":"<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 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[7],"tags":[],"class_list":["post-2003","post","type-post","status-publish","format-standard","hentry","category-python-tutorials"],"_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":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2003\/revisions"}],"predecessor-version":[{"id":2171,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2003\/revisions\/2171"}],"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}]}}