{"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-08-28T14:09:51","modified_gmt":"2026-08-28T12:09:51","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":"\n<p class=\"wp-block-paragraph\"><em>Learn how to create an SDK in Python step by step, build a Student SDK, package it with Node, and prepare it for publishing to NPM.<\/em><\/p>\n\n\n<h3>TL;DR<\/h3>\n<ul>\n<li>\n<p>Learn what an SDK is and how it differs from an API.<\/p>\n<\/li>\n<li>\n<p>Build a simple Student SDK in Python for managing student data.<\/p>\n<\/li>\n<li>\n<p>Create functions for adding and retrieving student records.<\/p>\n<\/li>\n<li>\n<p>Wrap the Python SDK in a Node application using <code>python-shell<\/code>.<\/p>\n<\/li>\n<li>\n<p>Prepare the SDK for publishing as an NPM package.<\/p>\n<\/li>\n<\/ul>\n<p>In this tutorial, you will learn how to build an SDK \u2013 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:\/\/kindsonthegenius.com\/blog\/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\u2019ll 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>\n\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\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">cursor<\/span>():\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()\n\nc <span style=\"color: #333333;\">=<\/span> cursor()\n\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>)\nc<span style=\"color: #333333;\">.<\/span>connection<span style=\"color: #333333;\">.<\/span>close()\n\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">add_student<\/span>(student):\n        c <span style=\"color: #333333;\">=<\/span> cursor()\n        <span style=\"color: #008800; font-weight: bold;\">with<\/span> c<span style=\"color: #333333;\">.<\/span>connection:\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))\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span>  c<span style=\"color: #333333;\">.<\/span>lastrowid\n\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> cursor()\n    students <span style=\"color: #333333;\">=<\/span> []\n    <span style=\"color: #008800; font-weight: bold;\">with<\/span> c<span style=\"color: #333333;\">.<\/span>connection:\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>):\n            students<span style=\"color: #333333;\">.<\/span>append(student)\n    c<span style=\"color: #333333;\">.<\/span>connection<span style=\"color: #333333;\">.<\/span>close()\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> students\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:\/\/kindsonthegenius.com\/blog\/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\u2013 Create a new directory in your current directory.\u00a0 I call it nodeapi.<\/p>\n<p><strong>Step 2<\/strong>\u00a0\u2013 Navigate inside your new directory and install the python-shell package<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">npm i python-shell\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong> \u2013 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>)\n\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) {\n  <span style=\"color: #008800; font-weight: bold;\">if<\/span> (err) <span style=\"color: #008800; font-weight: bold;\">throw<\/span> err;\n  console.log(<span style=\"background-color: #fff0f0;\">'Command executed sucessfully'<\/span>);\n});\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong> \u2013 You can now run your node application using the command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">node index.ts\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<h3>FAQs<\/h3>\n<p><strong>What is an SDK in Python?<\/strong><\/p>\n<p>An SDK in Python is a collection of tools, functions, and code that helps developers build applications or work with a particular system or service.<\/p>\n<p><strong>What is the difference between an API and an SDK?<\/strong><\/p>\n<p>An API exposes functionality that applications can communicate with, while an SDK provides reusable building blocks that developers can use when creating applications.<\/p>\n<p><strong>How do you create an SDK in Python?<\/strong><\/p>\n<p>You can create an SDK by organizing reusable Python classes and functions that provide the functionality developers need without requiring them to work directly with the underlying implementation.<\/p>\n<p><strong>Can a Python SDK be published to NPM?<\/strong><\/p>\n<p>This tutorial demonstrates wrapping a Python SDK inside a Node application so it can be packaged as a Node package and prepared for publishing to NPM.<\/p>\n<p><strong>What is <code>python-shell<\/code> used for?<\/strong><\/p>\n<p><code>python-shell<\/code> allows a Node application to execute Python scripts, making it possible to connect the Node package with the Python SDK.<\/p>\n<h3>Final Thoughts<\/h3>\n<p>Building an SDK in Python provides a practical way to package reusable functionality for developers. In this example, the Student SDK provides functions for managing student records without exposing REST API endpoints.<\/p>\n<p>The tutorial also demonstrates how a Python SDK can be connected to a Node application using <code>python-shell<\/code>, providing a path toward packaging it as an NPM package. This helps illustrate the relationship between APIs, SDKs, Python packages, and Node packages.<\/p>","protected":false},"excerpt":{"rendered":"<p>###<\/p>\n<p>Learn how to create an SDK in Python step by step, build a Student SDK, package it with Node, and prepare it for publishing to NPM.<\/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-2004","post","type-post","status-publish","format-standard","hentry","category-python-tutorials"],"acf":[],"_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":3,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2004\/revisions"}],"predecessor-version":[{"id":2564,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2004\/revisions\/2564"}],"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}]}}