{"id":2004,"date":"2022-11-09T12:00:00","date_gmt":"2022-11-09T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/how-to-create-an-sdk-in-python-and-publish-to-npm-step-by-step\/"},"modified":"2026-07-05T03:26:42","modified_gmt":"2026-07-05T01:26:42","slug":"how-to-create-an-sdk-in-python-and-publish-to-npm-step-by-step","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-create-an-sdk-in-python-and-publish-to-npm-step-by-step\/","title":{"rendered":"How to Create an SDK in Python and Publish to NPM \u2013 Step by Step"},"content":{"rendered":"<p>In this tutorial, you will learn how to build an SDK &#8211; step by step. We would build an SDK using Python and you can also used the same procedure to build SDK with other languages.<\/p>\n<p>This tutorial follows from the previous tutorial on <a href=\"https:\/\/www.kindsonthegenius.com\/how-to-create-an-api-in-python-with-flask-step-by-step\/\" target=\"_blank\" rel=\"noopener\">How to Build an API in Python<\/a>. You can review it as well. In that tutorial, we build an API for managing Student data (performing CRUD operations). In this tutorial, we would build an SDK also for managing Student data.<\/p>\n<p>By going through these two tutorials, you&#8217;ll clearly understand the difference between an API and an SDK.<\/p>\n<p>We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">What is an SDK?<\/a><\/li>\n<li><a href=\"#t2\">The Student SDK<\/a><\/li>\n<li><a href=\"#t3\">Packaging Python SDK With Node<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. What is an SDK?<\/strong><\/h4>\n<p>An SDK (Software Development Tools) is a set of tools provided to help developers in creating applications or other informations systems.<\/p>\n<p>In other words, an SDK is set of building blocks or developments tools<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. The Student SDK<\/strong><\/h4>\n<p>The SDK would consists of functions to manage Student data. But this time, we would not be exposing any REST API endpoints.<\/p>\n<p>There are three files<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/KindsonTheGenius\/PythonSDKDemo\/blob\/master\/student.py\" target=\"_blank\" rel=\"noopener\">student.py<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/KindsonTheGenius\/PythonSDKDemo\/blob\/master\/student_sdk.py\" target=\"_blank\" rel=\"noopener\">student-sdk.py<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/KindsonTheGenius\/PythonSDKDemo\/blob\/master\/sdk_client.py\" target=\"_blank\" rel=\"noopener\">sdk-client.py<\/a><\/li>\n<\/ul>\n<p>You can get these files following the links to the GitHub repository.<\/p>\n<p>Part of the student-sdk.py file is given below.<\/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\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\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">cursor<\/span>():\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> sqlite3<span style=\"color: #333333;\">.<\/span>connect(<span style=\"background-color: #fff0f0;\">\"student.db\"<\/span>)<span style=\"color: #333333;\">.<\/span>cursor()\r\n\r\nc <span style=\"color: #333333;\">=<\/span> cursor()\r\n\r\nc<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">'CREATE TABLE IF NOT EXISTS STUDENTS(id TEXT, firstname TEXT, lastname TEXT, department TEXT)'<\/span>)\r\nc<span style=\"color: #333333;\">.<\/span>connection<span style=\"color: #333333;\">.<\/span>close()\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">add_student<\/span>(student):\r\n        c <span style=\"color: #333333;\">=<\/span> cursor()\r\n        <span style=\"color: #008800; font-weight: bold;\">with<\/span> c<span style=\"color: #333333;\">.<\/span>connection:\r\n            c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">'INSERT INTO STUDENTS VALUES(?, ?, ?, ?)'<\/span>,(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        <span style=\"color: #008800; font-weight: bold;\">return<\/span>  c<span style=\"color: #333333;\">.<\/span>lastrowid\r\n\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> cursor()\r\n    students <span style=\"color: #333333;\">=<\/span> []\r\n    <span style=\"color: #008800; font-weight: bold;\">with<\/span> c<span style=\"color: #333333;\">.<\/span>connection:\r\n        <span style=\"color: #008800; font-weight: bold;\">for<\/span> student <span style=\"color: #000000; font-weight: bold;\">in<\/span> c<span style=\"color: #333333;\">.<\/span>execute(<span style=\"background-color: #fff0f0;\">'SELECT * FROM STUDENTS'<\/span>):\r\n            students<span style=\"color: #333333;\">.<\/span>append(student)\r\n    c<span style=\"color: #333333;\">.<\/span>connection<span style=\"color: #333333;\">.<\/span>close()\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> students\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The code below provides the methods for adding a student record and getting list of student records.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Packaging Python SDK with Node<\/strong><\/h4>\n<p>We would also like to package this SDK as a Node package so we can publish it to NPM. I already made a tutorial on how to publish Node application to NPM. See link below.<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/how-to-publish-package-to-npm-step-by-step\/\" target=\"_blank\" rel=\"noopener\">How to publish to NPM<\/a><\/p>\n<p>Now to publish Python script, we would have to wrap it inside a Node application.\u00a0 Follow the steps below<\/p>\n<p><strong>Step 1<\/strong>\u00a0&#8211; Create a new directory in your current directory.\u00a0 I call it nodeapi.<\/p>\n<p><strong>Step 2<\/strong>\u00a0&#8211; Navigate inside your new directory and install the python-shell package<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">npm i python-shell\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong> &#8211; Create a typescript file (index.ts but can be any name). The content of the file is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">let<\/span> {PythonShell} <span style=\"color: #333333;\">=<\/span> require(<span style=\"background-color: #fff0f0;\">'python-shell'<\/span>)\r\n\r\nPythonShell.run(<span style=\"background-color: #fff0f0;\">'..\/sdk_client.py'<\/span>, <span style=\"color: #008800; font-weight: bold;\">null<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span> (err) {\r\n  <span style=\"color: #008800; font-weight: bold;\">if<\/span> (err) <span style=\"color: #008800; font-weight: bold;\">throw<\/span> err;\r\n  console.log(<span style=\"background-color: #fff0f0;\">'Command executed sucessfully'<\/span>);\r\n});\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong> &#8211; You can now run your node application using the command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">node index.ts\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>So with this, you can just follow the procedure to publish your SDK to NPM. We would go through the process in the next tutorial.<\/p>\n<p>Also check my YouTube Channel, <a href=\"https:\/\/www.youtube.com\/kindsonthetechpro\" target=\"_blank\" rel=\"noopener\">Kindson The Tech Pro<\/a> and <a href=\"https:\/\/www.youtube.com\/c\/KindsonTheGenius\" target=\"_blank\" rel=\"noopener\">Kindson The Genius<\/a> for video tutorials.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to build an SDK &#8211; step by step. We would build an SDK using Python and you can &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-2004","post","type-post","status-publish","format-standard","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2004","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=2004"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2004\/revisions"}],"predecessor-version":[{"id":2172,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2004\/revisions\/2172"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=2004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=2004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=2004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}