Automate SDK creation for FastAPI APIs
Supported SDK languages:
TypeScript / Javascript | Java / Kotlin | Python | C# | Go | PHP |
---|---|---|---|---|---|
✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
- Before getting started make sure you have a liblab account and the liblab CLI installed.
- If you don't have an existing project but want to try out liblab then check out our standalone tutorials.
Providing an SDK for your API can significantly ease the development process for users, ensuring quicker integration and encouraging wider adoption due to the streamlined development experience.
In this doc you'll learn how to take your existing FastAPI API and generate user-friendly SDKs for most major programming languages using the liblab SDK generator. After a short bit of setup you'll have a pipeline that will automatically generate SDKs for your API whenever you make changes.
Initializing the SDK
First, create a new directory to store your SDK. This directory should be separate from your existing project directory:
mkdir FastAPI-sdk
cd FastAPI-sdk
And initialize your project to create a liblab.config.json
file:
liblab init
Getting an OpenAPI Spec File
In order to auto-generate SDKs from a FastAPI project you need an OpenAPI Spec File. The OpenAPI file contains information that describes your API like servers, paths, operations, parameters, responses, and security schemas.
FastAPI automatically generates your API spec but it may be necessary to make small changes to your application to produce the best output. Simply adjust your FastAPI
app to add the arguments shown below. Most importantly your servers
array should contain at least one object with url
and description
properties.
The url
should be set to the root url of your API. Typically during this stage you would set this to a url for local development. This URL can always be changed later and liblab offers many options for configuring your server URL environment.
from fastapi import FastAPI
from pydantic import BaseModel
import json
# Create a FastAPI instance
app = FastAPI(
title="My API's Name",
description="A description of my API",
version="1.0.0",
servers=[
{"url": "http://localhost:8000", "description": "Local Server"}
]
)
Once you're done restart your FastAPI server and navigate to the /openapi.json
endpoint on your server (ex. http://localhost:8000/openapi.json
) to view your OpenAPI spec.
If an OpenAPI spec appears then you're ready to proceed. Go ahead and save your openapi.json
file to the liblab project directory that you made during the Initializing step. This is typically something like:
cd ../FastAPI-sdk
curl -o openapi.json http://localhost:8000/openapi.json
While generating an initial OpenAPI spec with FastAPI is simple and straightforward there are many more configuration options available to you. Here are a few resources you may be interested in:
- For details on configuring and using FastAPI, check the official documentation.
- To learn how to add metadata and configure documentation URLs refer to the FastAPI documentation on Metadata and Docs URLs.
- If you need to extend or customize the OpenAPI schema generated by FastAPI checkout their guide on Extending OpenAPI.
Configuring liblab
Now you'll need to make some minor updates to your liblab.config.json
file in your FastAPI-sdk folder:
- Point the
specFilePath
parameter to the location of your OpenAPI spec file (ex../openapi.json
). - Specify the
baseUrl
of your API. This is the URL that the SDK will use to make requests to your API.
The top of the file should then looks something like this:
{
"sdkName": "FastAPI-sdk",
"apiVersion": "1.0.0",
"apiName": "FastAPI-api",
"specFilePath": "./openapi.json",
"baseUrl": "http://localhost:PORT",
"languages": [
"go",
"java",
"python",
"typescript",
"csharp"
],
"auth": [
"bearer"
]
}
liblab's SDK generator supports many more advanced URL and environment configuration options than the basic configuration shown here.
Explore the configuration documentation to discover all the available settings and enhancements or review the SDK customization options for tailored adjustments.
Generate the SDK
During build you might see warnings about the OpenAPI spec. These are often minor issues that can be fixed later.
Now that you have an OpenAPI spec file and have finished setting the liblab.config.json
file, it's time to generate our SDK:
liblab build
The CLI will validate the OpenAPI spec and notify you about any issues with it or the liblab.config.json
.
The output will look something like this:
✓ 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/FastAPI-sdk/output/api-schema-validation.json with the full linting results
Detected 8 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. (3 occurrences)
⚠ Operation tags must be defined in global tags. (3 occurrences)
? 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)
You can go ahead and confirm by typing Y
.
Next you'll see the builds started and once they're done you'll see a message like this:
Ignoring the spec errors and attempting to build with the spec
Your SDKs are being generated. Visit the liblab portal (https://app.liblab.com/apis/FastAPI-sdk/builds/1234) to view more details on your build(s).
✓ C# built
✓ Go built
✓ Java built
✓ Python built
✓ TypeScript built
✓ Generate package-lock.json for TypeScript
Successfully generated SDKs for Python, Java, Go, TypeScript, C#. ♡ You can find them inside: /Users/username/projects/FastAPI-sdk/output
If we go inside the output
directory, we will see a directory for each of our SDKs:
ls output/
api-schema-validation.json go python
csharp java typescript
Try out your SDK
The following instructions assume you have already set up the respective development environment for the language you are testing. If necessary refer to each language's official documentation before proceeding.
Learn more about the language versions liblab generated SDKs support.
The generated SDKs are intended to be deployed to package managers for end users. The instructions below to test your SDK locally will differ from user-facing instructions.
- TypeScript / JavaScript
- Python
- Java
- C#
cd output/typescript/examples
npm run setup
npm run start
cd output/python/examples
chmod +x install.sh
./install.sh
source .venv/bin/activate
python3 sample.py
cd output/java/example
chmod +x run.sh
./run.sh
cd output/csharp/Example
dotnet run --framework net9.0
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: