> ## Documentation Index
> Fetch the complete documentation index at: https://docs-staging-quickstart-revamp.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Integrating Auth0's Management API endpoints with your PHP applications.

# PHP: Using the Management API with Auth0-PHP

The Auth0 PHP SDK provides a `Auth0\SDK\API\Management` class, which houses the methods you can use to access the [Management API](https://auth0.com/docs/api/management/v2) and perform operations on your Auth0 tenant. Using this interface, you can easily:

* Search for and create users
* Create and update Applications
* Retrieve log entries
* Manage rules

... and much more. See our [API reference](https://auth0.com/docs/api/management/v2) for information on what's possible!
Authentication

To use the <Tooltip tip="Management API: A product to allow customers to perform administrative tasks." cta="View Glossary" href="/docs/glossary?term=Management+API">Management API</Tooltip>, you must authenticate one of two ways:

* For temporary access or testing, you can [manually generate an API token](/docs/secure/tokens/access-tokens/management-api-access-tokens) and save it in your `.env` file.
* For extended access, you must create and execute and Client Credentials grant when access is required. This process is detailed on the [Authentication API page](/docs/libraries/auth0-php/using-the-authentication-api-with-auth0-php).

Regardless of the method, the token generated must have the scopes required for the operations your app wants to execute. Consult the [API documentation](https://auth0.com/docs/api/management/v2) for the scopes required for the specific endpoint you're trying to access.

To grant the scopes needed:

1. Go to [APIs](https://manage.auth0.com/#/apis) > Auth0 Management API > **Machine to Machine Applications** tab.
2. Find your Application and authorize it.
3. Click the arrow to expand the row and select the scopes required.

Now you can authenticate one of the two ways above and use that token to perform operations:

```php lines
// 👆 We're continuing from the "getting started" guide linked in "Prerequisites" above. Append this to the index.php file you created there.

if (isset($env['AUTH0_MANAGEMENT_API_TOKEN'])) {
    $auth0->configuration()->setManagementToken($env['AUTH0_MANAGEMENT_API_TOKEN']);
}

// Create a configured instance of the `Auth0\SDK\API\Management` class, based on the configuration we setup the SDK ($auth0) using.
// If no AUTH0_MANAGEMENT_API_TOKEN is configured, this will automatically perform a client credentials exchange to generate one for you, so long as a client secret is configured.
$management = $auth0->management();
```

The `Management` class stores access to endpoints as factory methods of its instances, for example `$management->users()` returns an instance of `Auth0\SDK\API\Management\Users` that you can use to interact with the /users Management API endpoints.

### Example - Search Users by Email

This endpoint is documented [here](https://auth0.com/docs/api/management/v2#!/Users/get_users).

```php lines
// 👆 We're continuing from the code above. Append this to your source code file.

$response = $management->users()->getAll(['q' => 'josh']);

// Does the status code of the response indicate failure?
if ($response->getStatusCode() !== 200) {
    die("API request failed.");
}

// Decode the JSON response into a PHP array:
$response = json_decode(response->getBody()->__toString(), true, 512, JSON_THROW_ON_ERROR);

if (! empty($response)) {
    echo '<h2>User Results</h2>';

    foreach ($response as $result) {
        printf(
            '<p><strong>%s</strong> &lt;%s&gt; - %s</p>',
            !empty($result['nickname']) ? $result['nickname'] : 'No nickname',
            !empty($result['email']) ? $result['email'] : 'No email',
            $result['user_id']
        );
    }
}
```

### Example - Get All Clients

This endpoint is documented [here](https://auth0.com/docs/api/management/v2#!/Clients/get_clients).

```php lines
// 👆 We're continuing from the code above. Append this to your source code file.

$response = $management->clients()->getAll(['q' => 'josh']);

// Does the status code of the response indicate failure?
if ($response->getStatusCode() !== 200) {
    die("API request failed.");
}

// Decode the JSON response into a PHP array:
$response = json_decode(response->getBody()->__toString(), true, 512, JSON_THROW_ON_ERROR);

if (! empty($response)) {
    echo '<h2>Get All Clients</h2>';

    foreach ($response as $result) {
        printf(
            '<p><strong>%s</strong> - %s</p>',
            $result['name'],
            $result['client_id']
        );
    }
}
```

## Learn more

* [PHP: Getting Started using Auth0-PHP](/docs/libraries/auth0-php)
* [PHP: Logging in, out, and returning user profiles with Auth0-PHP](/docs/libraries/auth0-php/auth0-php-basic-use)
* [PHP: Using the Authentication API with Auth0-PHP](/docs/libraries/auth0-php/using-the-authentication-api-with-auth0-php)
* [PHP: Validating JWTs (JSON Web Tokens) with Auth0-PHP](/docs/libraries/auth0-php/validating-jwts-with-auth0-php)
* [PHP: Troubleshooting your Auth0-PHP integration](/docs/libraries/auth0-php/troubleshoot-auth0-php-library)
