Link Search Menu Expand Document (external link)

Build SDKs for PHP

Required tools

Target project requirements

Before you can run the target project, you will need to initialize it. After initializing the test project, you will need to add references to the abstractions, authentication, http, serialization JSON, and serialization Text packages.

Creating target projects

Note: you can use an existing project if you have one, in that case, you can skip the following section.

Execute the following commands in the directory where you want to create a new project.

composer init

Adding dependencies

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

Add the following to your composer.json to set your namespaces correctly:

"autoload": {
    "psr-4": {
        "GetUser\\Client\\": "client/"

Only the first package, microsoft/kiota-abstractions, is required. The other packages provide default implementations that you can choose to replace with your own implementations if you wish.

Generating the SDK

Kiota generates SDKs from OpenAPI documents. Create a file named getme.yml and add the contents of the Sample OpenAPI description.

You can then use the Kiota command line tool to generate the SDK classes.

kiota -l PHP -d ../getme.yml -c GraphApiClient -n GetUser\Client -o ./client

To ensure the newly generated classes can be imported, update the autoload paths using:

composer dumpautoload

Creating an application registration

Note: this step is required if your client will be calling APIs that are protected by the Microsoft Identity Platform like Microsoft Graph.

Follow the instructions in Register an application for Microsoft identity platform authentication to get an application ID (also know as a client ID).

Creating the client application

Create a file in the root of the project named GetUser.php and add the following code. Replace the $tenantId, $clientId, $clientSecret with your credentials from the previous step. See Get access on behalf of a user for details on how to get the $authorizationCode and set the $redirectUri.


use GetUser\Client\GraphApiClient;
use Microsoft\Kiota\Abstractions\ApiException;
use Microsoft\Kiota\Authentication\Oauth\AuthorizationCodeContext;
use Microsoft\Kiota\Authentication\PhpLeagueAuthenticationProvider;
use Microsoft\Kiota\Http\GuzzleRequestAdapter;

require __DIR__.'/vendor/autoload.php';

try {
    $tenantId = 'tenantId';
    $clientId = 'clientId';
    $clientSecret = 'secret';
    $authorizationCode = 'authCode';
    $redirectUri = 'uri';

    $scopes = ['User.Read'];

    $tokenRequestContext = new AuthorizationCodeContext(

    $authProvider = new PhpLeagueAuthenticationProvider($tokenRequestContext, $scopes);
    $requestAdapter = new GuzzleRequestAdapter($authProvider);
    $client = new GraphApiClient($requestAdapter);

    $me = $client->me()->get()->wait();
    echo "Hello {$me->getDisplayName()}, your ID is {$me->getId()}";

} catch (ApiException $ex) {
    echo $ex->getMessage();


  • If the target API doesn’t require any authentication, you can use the AnonymousAuthenticationProvider instead.
  • If the target API requires an Authorization: Bearer <token> header but doesn’t rely on the Microsoft Identity Platform, you can implement your own authentication provider by inheriting from BaseBearerTokenAuthenticationProvider.
  • If the target API requires any other form of authentication schemes, you can implement the AuthenticationProvider interface.

Executing the application

When ready to execute the application, execute the following command in your project directory.

php GetUser.php

See also