Walkthrough: Enabling and authorizing requests to the Authoring and Management API
You can explore the Authoring and Management API with an interactive browser-based GraphQL IDE or through HTTP requests.
Querying the GraphQL Authoring and Management API programmatically or using the GraphQL IDE requires authorization.
This walkthrough describes how to:
-
Enable the GraphQL IDE.
-
Obtain an access token.
-
Obtain an additional access token (optional).
-
Authorize the GraphQL IDE.
-
Authorize HTTP requests.
This walkthrough assumes you are running commands in PowerShell 7 or later. Earlier versions of PowerShell might not interpret the curl command correctly and can result in errors like those described here. If you encounter issues, ensure you are using PowerShell 7 or later, or run commands directly in a terminal that supports curl (such as Git Bash or WSL).
Enable the GraphQL IDE
To use the GraphQL IDE, you must enable it in your settings. You must have, at a minimum, the sitecore\Sitecore Client Users role.
The GraphQL IDE is intended to support development and exploratory use and is not required for normal operation of the GraphQL APIs.
To enable the GraphQL IDE:
-
In Deploy, create an environment variable named
Sitecore_GraphQL_ExposePlaygroundwith atruevalue. If already present, update the environment variable value totrue. -
For the environment variable to take effect, redeploy your environment.
Obtain an access token
You must obtain an access token to authorize the GraphQL IDE or HTTP requests to perform operations against the Authoring and Management API endpoint.
To obtain an access token:
-
Log in to your environment by running the
dotnet sitecore cloud logincommand in the Sitecore CLI and following the prompts. -
When logged in, the process stores an access token that you can use to set the necessary Authorization header for the GraphQL IDE and HTTP requests. From the
./sitecore/user.jsonfile, copy the value of theaccessTokenproperty and store it for later use,
If you do not require additional tokens or automation clients, continue to authorize the GraphQL IDE.
Obtain an additional access token (optional)
You can generate additional tokens if you require or prefer to use multiple tokens/automation clients, for example, one for HTTP requests and another for the GraphQL IDE.
Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.
To obtain an additional access token:
-
Create an automation client for your environment. By default, the authority URL is
https://auth.sitecorecloud.io, and the audience URL ishttps://api.sitecorecloud.io. -
Request a Bearer token with the following command:
Replace the placeholders as follows:
-
<authority-url>- with the value ofendpoints.xmCloud.authorityvalue from the.sitecore/user.jsonfile. -
<your-client-id>- with the value ofendpoints.xmCloud.clientIdvalue from the.sitecore/user.jsonfile. -
<your-client-secret>- with the value ofendpoints.xmCloud.clientSecretvalue from the.sitecore/user.jsonfile. -
<audience-url>- with the value ofendpoints.xmCloud.audiencevalue from the.sitecore/user.jsonfile.
If successful, the response is printed in the console.
-
-
From the response, copy the
access_tokenvalue without quotation marks and store the token for later use.
Pay attention to the expires_in property of the response. It indicates the JWT token expiration period. After that time, the token is invalid, and you must request a new token.
Authorize the GraphQL IDE
Before performing queries and mutations in the GraphQL IDE, you must authorize the IDE to perform operations.
To set the correct headers for the GraphQL IDE:
-
Open the IDE in the browser at
https://<your-server>/sitecore/api/authoring/graphql/ide/. -
In the IDE's lower-left pane, on the HTTP Headers tab, add the authorization header with the following format:
Replace the
<access-token>placeholder with the value you copied. -
In the IDE's endpoint field (to the right of the History tab), verify that the endpoint is
https://<your-instance>/sitecore/api/authoring/graphql/v1/. -
Verify your setup by running a query. For example:
If the configuration is correct, the response contains a list of your websites. For example:
Authorize HTTP requests
HTTP requests against the Authoring and Management API endpoint require authorization.
To authorize your HTTP requests:
-
Include the HTTP
Authorizationheader with theBearerauthentication scheme. Replace the<access-token>placeholder with the value of the access token. For example: