How to create an SDK for your FastAPI application
This tutorial applies to the following SDK languages:
TypeScript Java Python C# Go PHP ✅ ✅ ✅ ✅ ✅ ✅
Creating an SDK for your API makes it easier for developers to connect with your system, build their own projects, and use your API faster. This tutorial will show you how to set up an SDK for a FastAPI application using the liblab SDK generator.
Before getting started you'll want to make sure you have a liblab account and the liblab CLI installed.
You'll also need Python version 3.8 or higher installed.
Set up a FastAPI application
Start by creating a simple FastAPI application that will serve as the base for your SDK. Follow the steps to have a FastAPI application running locally:
Create a new folder for your project:
mkdir -p llamapi
cd llamapi
Run the appropriate script for your operating system to set up and activate a Python virtual environment:
- Mac / Linux
- Windows
python3 -m venv .venv
source .venv/bin/activate
python -m venv .venv
.venv/Scripts/Activate.ps1
Using a virtual environment isolates project dependencies, prevents conflicts, and avoids common pitfalls. For more details, refer to the Python venv documentation.
Install FastAPI by running this command:
pip3 install fastapi uvicorn
Inside the llamapi
folder, create a file named main.py
with the following code. The API will have only one GET endpoint at route /llamas
.
# llamapi/main.py
from fastapi import FastAPI
app = FastAPI()
# Define the SDK method name using operation_id
@app.get("/llamas", operation_id="get_llamas")
async def get_llamas():
return {"content": ["Libby the llama", "Paka the alpaca"]}
Run the FastAPI application using the following command:
uvicorn main:app --reload
Check that your API is running at http://localhost:8000/llamas
. By accessing the endpoint, you'll see the following result in your browser:
{
"content": [
"Libby the llama",
"Paka the alpaca"
]
}
You can also access the OpenAPI spec, which you can find at http://localhost:8000/openapi.json
. You'll use this OpenAPI spec to generate the SDK.
Configure liblab
Make sure to create your liblab SDK folder somewhere outside of the FastAPI project folder. You'll generally want your SDK and API to be separate projects in separate repositories.
Next, let's set up a liblab project so you can generate the SDK:
Create a folder for the SDK project:
mkdir -p llamapi-sdk
cd llamapi-sdk
Initialize the liblab CLI in this folder by running the following command. This will create a file called liblab.config.json
:
liblab init
Download the OpenAPI spec file from http://localhost:8000/openapi.json
and save it as openapi.json
:
curl -o openapi.json http://localhost:8000/openapi.json
In most cases, you can use a publicly available OpenAPI spec to generate the SDK while using liblab instead of downloading it. However, that URL must be publicly accessible. Since this is a development environment you'll download and save the spec file locally.
Edit the liblab.config.json
file and do the following:
- Change
"specFilePath"
to"./openapi.json"
- Set
"baseUrl"
to your API server URL (ie."http://localhost:8000"
) - (optional) You can also change the name of your SDK and API:
{
"sdkName": "llamapi-sdk",
"apiVersion": "1.0.0",
"apiName": "llamapi",
"specFilePath": "./openapi.json",
"baseUrl": "http://localhost:8000",
// ...
}
This guide demonstrates a simple single-server configuration. If you have a more complex server configuration refer to the URLs and environments documentation.
Explore the configuration documentation to discover all the available settings and enhancements.
Generate the SDK
Now that you have an OpenAPI spec file and the liblab CLI, it is time to generate the SDK.
Run the following command to generate the SDK:
liblab build
liblab CLI will check the config and OpenAPI file for any errors. You might expect something like:
✓ No issues detected in the liblab config file.
No hooks found, SDKs will be generated without hooks.
⚠ Validation succeeded with warnings
Created /Users/username/projects/llamapi-sdk/output/api-schema-validation.json with the full linting results
Detected 4 potential issues with the spec:
⚠ Info object must have "contact" object.
⚠ Info "description" must be present and non-empty string.
⚠ Operation "description" must be present and non-empty string.
⚠ Operation must have non-empty "tags" array.
? It is important to fix your spec before continuing with a build. Not fixing the spec may yield a subpar SDK and
documentation. Would you like to attempt to build the SDK anyway? (Y/n)
If there are no serious issues, confirm the build by typing Y
.
After the build finishes, you'll see in your terminal a message like this:
Your SDKs are being generated. Visit the liblab portal (https://app.liblab.com/apis/llamapi-sdk/builds/1234) to view more details on your build(s).
✓ Go built
✓ Java built
✓ Python built
✓ TypeScript built
✓ Generate package-lock.json for TypeScript
Successfully generated SDKs for Python, TypeScript, Java, Go. ♡ You can find them inside: /Users/username/projects/llamapi-sdk/output
You'll find your SDK files in the output
directory, which should have the following structure:
output/
├── api-schema-validation.json
├── java/
├── typescript/
├── go/
└── python/
Test the SDK
Great, your SDK is built. Every SDK also comes with a sample script that you can run to test the SDK. Let's test out the Python SDK.
From your terminal, open the directory output/python/examples
:
cd output/python/examples
Run one of the following scripts, depending on your operating system, to set up and activate a Python virtual environment:
- Mac / Linux
- Windows
chmod +x install.sh
./install.sh
source .venv/bin/activate
./install.cmd
.venv/Scripts/Activate.ps1
The command to activate venv may vary depending on your operating system and shell. If you encounter any issues, refer to Python's venv documentation to determine the correct venv command.
Run the sample.py
script to execute the sdk.llamas.get_llamas()
function:
python3 sample.py
You should see output similar to the API response you received previously from the /llamas
endpoint:
{'content': ['Libby the llama', 'Paka the alpaca']}
Just like that, you have successfully created an SDK that makes it easy for developers to adopt your products and services!
Next Steps
Now that you've packaged your SDKs you can learn how to integrate them with your CI/CD pipeline and publish them to their respective package manager repositories.
We currently have guides for: