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.
TL;DR
-
Learn what an SDK is and how it differs from an API.
-
Build a simple Student SDK in Python for managing student data.
-
Create functions for adding and retrieving student records.
-
Wrap the Python SDK in a Node application using
python-shell. -
Prepare the SDK for publishing as an NPM package.
In this tutorial, you will learn how to build an SDK – step by step. We would build an SDK using Python and you can also used the same procedure to build SDK with other languages.
This tutorial follows from the previous tutorial on How to Build an API in Python. 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.
By going through these two tutorials, you’ll clearly understand the difference between an API and an SDK.
We would cover the following:
1. What is an SDK?
An SDK (Software Development Tools) is a set of tools provided to help developers in creating applications or other informations systems.
In other words, an SDK is set of building blocks or developments tools
2. The Student SDK
The SDK would consists of functions to manage Student data. But this time, we would not be exposing any REST API endpoints.
There are three files
You can get these files following the links to the GitHub repository.
Part of the student-sdk.py file is given below.
import sqlite3 from student import Student def cursor(): return sqlite3.connect("student.db").cursor() c = cursor() c.execute('CREATE TABLE IF NOT EXISTS STUDENTS(id TEXT, firstname TEXT, lastname TEXT, department TEXT)') c.connection.close() def add_student(student): c = cursor() with c.connection: c.execute('INSERT INTO STUDENTS VALUES(?, ?, ?, ?)',(student.id, student.firstname, student.lastname, student.department)) return c.lastrowid def get_students(): c = cursor() students = [] with c.connection: for student in c.execute('SELECT * FROM STUDENTS'): students.append(student) c.connection.close() return students
The code below provides the methods for adding a student record and getting list of student records.
3. Packaging Python SDK with Node
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.
Now to publish Python script, we would have to wrap it inside a Node application. Follow the steps below
Step 1 – Create a new directory in your current directory. I call it nodeapi.
Step 2 – Navigate inside your new directory and install the python-shell package
npm i python-shell
Step 3 – Create a typescript file (index.ts but can be any name). The content of the file is given below:
let {PythonShell} = require('python-shell') PythonShell.run('../sdk_client.py', null, function (err) { if (err) throw err; console.log('Command executed sucessfully'); });
Step 4 – You can now run your node application using the command:
node index.ts
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.
Also check my YouTube Channel, Kindson The Tech Pro and Kindson The Genius for video tutorials.
FAQs
What is an SDK in Python?
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.
What is the difference between an API and an SDK?
An API exposes functionality that applications can communicate with, while an SDK provides reusable building blocks that developers can use when creating applications.
How do you create an SDK in Python?
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.
Can a Python SDK be published to NPM?
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.
What is python-shell used for?
python-shell allows a Node application to execute Python scripts, making it possible to connect the Node package with the Python SDK.
Final Thoughts
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.
The tutorial also demonstrates how a Python SDK can be connected to a Node application using python-shell, providing a path toward packaging it as an NPM package. This helps illustrate the relationship between APIs, SDKs, Python packages, and Node packages.