| title | description | author | ms.author | ms.topic | ms.date |
|---|---|---|---|---|---|
Build API clients for Python |
Learn how use Kiota to build API clients in Python. |
jasonjoh |
jasonjoh |
quickstart |
03/24/2023 |
In this tutorial, you build a sample app in Python that calls a REST API that doesn't require authentication.
- Python 3.9+
- pip 20.0+
- asyncio or any other supported async environment, for example, AnyIO, Trio.
Create a directory to contain the new project.
Create and start a virtual environment.
python3 -m venv .venv # directories starting with '.' will be hidden)Activate the virtual environment
source .venv/bin/activate # macOS/Linux.venv\Scripts\activate # WindowsVerify the virtual environment
python --versionIn case you're adding a Kiota client to an existing project, the following configuration is required:
- pyproject.toml > project > requires-python set to ">=3.9".
Before you can compile and run the generated API client, you need to make sure the generated source files are part of a project with the required dependencies. Your project must have a reference to the abstraction package. Additionally, you must either use the Kiota default implementations or provide your own custom implementations of the following packages:
- HTTP (Kiota default HTTPX-based implementation)
- JSON serialization (Kiota default)
- Text serialization (Kiota default)
For this tutorial, use the default implementations.
Run the following commands to get the required dependencies:
pip install microsoft-kiota-abstractions
pip install microsoft-kiota-http
pip install microsoft-kiota-serialization-json
pip install microsoft-kiota-serialization-text
pip install microsoft-kiota-serialization-form
pip install microsoft-kiota-serialization-multipartTip
It is recommended to use a package manager/virtual environment to avoid installing packages system wide. Read more here.
Kiota generates API clients from OpenAPI documents. Create a file named posts-api.yml and add the following.
:::code language="yaml" source="~/code-snippets/get-started/quickstart/posts-api.yml":::
This file is a minimal OpenAPI description that describes how to call the /posts endpoint in the JSONPlaceholder REST API.
You can then use the Kiota command line tool to generate the API client classes.
kiota generate -l python -c PostsClient -n client -d ./posts-api.yml -o ./clientTip
Add --exclude-backward-compatible
if you want to reduce the size of the generated client and are not concerned about
potentially source breaking changes with future versions of Kiota when updating the client.
Create a file in the root of the project named main.py and add the following code.
:::code language="python" source="~/code-snippets/get-started/quickstart/python/main.py" id="ProgramSnippet":::
Note
The JSONPlaceholder REST API doesn't require any authentication, so this sample uses the AnonymousAuthenticationProvider. For APIs that require authentication, use an applicable authentication provider.
To start the application, run the following command in your project directory.
python3 main.py- kiota-samples repository contains the code from this guide.
- Microsoft Graph sample using Microsoft identity platform authentication
- ToDoItem Sample API implements a sample OpenAPI in ASP.NET Core and sample clients in multiple languages.