Supercharge Full-Stack Development with FastAPI & SDK Generation
Full source code for this article can be found on Github
FastAPI’s integration with Pydantic has set a high developer experience standard when creating and testing HTTP APIs. With the following python code you get authentication, model validation, native enum support, and interactive documentation that lets you tinker and test your API as you develop.
However, tinkering and manual testing can only get you so far, and API developers spend a lot of time writing API clients to integrate and distribute their APIs. Let’s say I am a full stack developer an want to build a basic NextJS app to list, browse, and add books using the bookstore API above. Every time I alter or extend the API, I need to update my typescript client to match the new models, routes, authentication, etc. Every change to the backend, requires a change to the frontend!
This is where SDK generators come in. I’ll be using a free SDK generator called Sideko to show how SDK generation can simplify your full stack work.
- Install
sideko-py
from pypi
- Log in / create an account with Sideko to be issued an API key
- Add this code to the FastAPI server to generate a Typescript SDK on every change
We now have a fully typed Typescript SDK that updates as our server does! Sideko’s SDK generation is so fast you should not notice a change in your development server’s startup time.
We can now install the SDK in our frontend:
And use our SDK in react:
This component renders as:
Looking at the generated typescript types, we can see that the pydantic models in
our server code have been generated in native typescript (lib/schemas.ts
)
which gives us automatic type-hinting and confidence when writing our typescript:
Let’s quickly add a new endpoint, to see how this would work as we develop the API:
The SDK will automatically regenerate, and in seconds getBook
is fully typed and ready to
use in the front end:
You can find the full source code on Github,
along with a suggested exercise to extend the API and thereby the SDK. Clone it and try it out for yourself!
You can also generate SDKs in Python, Ruby, Rust, or Go using the sideko
CLI that gets installed with sideko-py
.
In my next post I’ll be using the Sideko test generation feature to run unit tests against the FastAPI server, testing both the SDK and the server in one shot!
Happy generating!