November 8th, 2023

Generate tailored Python and PHP API clients for any API with Kiota

Working on data science, AI or back-end web applications usually requires managing different APIs with its HTTP clients, which can be inconvenient as different APIs build API clients differently. With Kiota you don’t need to learn a new API client for every API, instead, you can now generate a consistent client experience across all the APIs you are using, regardless of their nature, all you need is an OpenAPI file.

To learn more about OpenAPI, see Home – OpenAPI Initiative (openapis.org)

The experience is available as a command line tool and as a Visual Studio Code extension. Kiota eliminates the need to take a dependency on a different API client for every API that you need to call, as well as limiting the size of the client to the exact API surface area you’re interested in, improving the efficiency of the client.

In a matter of seconds, Kiota can:

  • Search for API descriptions
  • Filter and select the API endpoints you need
  • Generate models and a chained method API surface in the language of your choice: C#, Go, PHP, Python (and preview or experimental support for Java, CLI, TypeScript, Ruby and Swift).
  • Call the API with the new client.

Generating Python API clients

If you are working on data science, AI or cybersecurity projects, you may need to integrate different APIs. You can generate a consistent API client experience for all those APIs using Kiota.

Step 1: Install Kiota

First, install Kiota as a command-line tool in your machine.

dotnet tool install --global Microsoft.OpenApi.Kiota

Step 2: Prerequisites

To generate Python clients, install these required tools:

Step 3: Install dependencies

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

Step 4: Generate the API client

Use the OpenAPI file to generate an API client with Kiota.

kiota generate -l python -c PostsClient -n client -d https://raw.githubusercontent.com/microsoft/kiota-samples/main/get-started/quickstart/posts-api.yml -o ./client

Step 5: Call the API

All Kiota generated API clients will share a consistent usability experience.

import asyncio
from kiota_abstractions.authentication.anonymous_authentication_provider import (
AnonymousAuthenticationProvider)
from kiota_http.httpx_request_adapter import HttpxRequestAdapter
from client.posts_client import PostsClient
from client.models.post import Post
async def main():
# You may need this if your're using asyncio on windows
# See: https://stackoverflow.com/questions/63860576
# asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# API requires no authentication, so use the anonymous
# authentication provider
auth_provider = AnonymousAuthenticationProvider()
# Create request adapter using the HTTPX-based implementation
request_adapter = HttpxRequestAdapter(auth_provider)
# Create the API client
client = PostsClient(request_adapter)
# GET /posts
all_posts = await client.posts.get()
print(f"Retrieved {len(all_posts)} posts.")
# Run main
asyncio.run(main())

See the complete tutorial here: Build API clients for Python | Microsoft Learn

Generating PHP API clients

If you are working with back-end web applications or integrating with a Learning Management System (LMS), you may need to integrate and manage different APIs in your project. With Kiota, you can generate a consistent API client experience across all APIs, simplifying the code in your app.

Step 1: Install Kiota

First, install Kiota as a command-line tool in your machine.

dotnet tool install --global Microsoft.OpenApi.Kiota

Step 2: Prerequisites

To generate PHP clients, install these required tools:

Step 3: Install dependencies

Run the following commands to get the required dependencies.

composer require microsoft/kiota-abstractions
composer require microsoft/kiota-http-guzzle
composer require microsoft/kiota-serialization-json
composer require microsoft/kiota-serialization-text

Step 4: Generate the API client

Use the OpenAPI file to generate an API client with Kiota.

kiota generate -l PHP -d https://raw.githubusercontent.com/microsoft/kiota-samples/main/get-started/quickstart/posts-api.yml -c PostsApiClient -n KiotaPosts\Client -o ./client

Step 5: Call the API

All Kiota generated API clients will share a consistent usability experience.

<?php
use KiotaPosts\Client\PostsApiClient;
use KiotaPosts\Client\Models\Post;
use Microsoft\Kiota\Abstractions\ApiException;
use Microsoft\Kiota\Abstractions\Authentication\AnonymousAuthenticationProvider;
use Microsoft\Kiota\Http\GuzzleRequestAdapter;
require __DIR__.'/vendor/autoload.php';
try {
$authProvider = new AnonymousAuthenticationProvider();
$requestAdapter = new GuzzleRequestAdapter($authProvider);
$client = new PostsApiClient($requestAdapter);
// GET /posts
$allPosts = $client->posts()->get()->wait();
$postCount = sizeof($allPosts);
echo "Retrieved {$postCount} posts.\n";
}catch (ApiException $ex) {
echo $ex->getMessage();
}
?>

See the complete tutorial here: Build API clients for PHP | Microsoft Learn

You can also run Kiota directly from Visual Studio Code, using the VS Code extension.

Also new in Kiota

We are also happy to announce new features of Kiota:

  • Added support for API Manifests.
  • Added support for raw URL in the fluent API surface.
  • Added support for OpenTelemetry tracing.
  • Added support for enum query parameter types.
  • Added support for multiple content type request bodies.
  • Added support for multiple content type responses.
  • Added a json output for the Kiota info command.

For details, read the release notes: Release v1.8.0 · microsoft/kiota (github.com), Release v1.7.0 · microsoft/kiota (github.com), Release v1.6.0 · microsoft/kiota (github.com)

Resources

Happy coding!

Author

0 comments

Discussion are closed.