Tailwind Logo

Retrieve data using API keys

Content Hub ONE

Published: 2024-08-02

In order to retrieve information about the contents, we will use API keys to retrieve data. This time, we will create two keys and check on the data acquisition.

Creating API Keys

To create a key, select the API Keys item from the Settings menu, and you will see the following situation where no keys are displayed.

choneapi01.png

When you click the Create API Key button, you will be asked to confirm the name and type of the key, as shown below. First, create a key for the Type of Delivery.

choneapi02.png

Click the Save button to display the API key. Note that this key can be obtained while it is displayed on this screen, but it cannot be taken from the list.

choneapi03.png

Click on Open GFraphQL IDE in the lower left corner of the dialog to access GraphQL at the following URL

One more thing, get the key for Preview.

choneapi04.png

Similarly, the key can only be retrieved at this time, so please copy and save it. The URL of the GraphQL IDE displayed in the Preview dialog is as follows.

Retrieve data using API keys

GraphQL endpoints for retrieving data on content managed by Content Hub ONE are listed on the following pages.

This time, two keys were created, but with different endpoints for Delivery and Preview. First, access the Preview API IDE.

choneapi05.png

The screen shows an error. To avoid this error, you need to open HTTP HEADERS and set the key there.To write the key, insert the key obtained above into the following Json.

JSON
{
    "X-GQL-Token":"your-preview-api-key"
}

After setting the key, the error message disappeared from the screen on the right side as shown below.

choneapi06.png

The following query was actually created on the left screen using Query in GraphQL query examples.

GraphQL
{
  allBlog {
    total
    results {
      name
      id
      title
      description
    }
  }
}

As a result, the following data were obtained

JSON
{
  "data": {
    "allBlog": {
      "total": 2,
      "results": [
        {
          "name": "Introducing Sitecore Composable DXP Products",
          "id": "bthW8EnPU0-tdSeXOdMdSQ",
          "title": "Introducing Sitecore Composable DXP Products",
          "description": "Learn about the powerful features and benefits of Sitecore Composable DXP Products."
        },
        {
          "name": "Introduction to the Blog",
          "id": "jPUxNefHAkW31dYrXPwuUg",
          "title": "Welcome to Our Blog",
          "description": "Explore the latest trends, insights, and stories in our blog."
        }
      ]
    }
  }
}

In preview, we are able to obtain content with the statuses of Published and Draft.

Next, switch to Delivery's GraphQL IDE and set the HTTP HEADERS to the correct value. Then, using the same query we threw for Preview, we get the following Json data

JSON
{
  "data": {
    "allBlog": {
      "total": 1,
      "results": [
        {
          "name": "Introducing Sitecore Composable DXP Products",
          "id": "bthW8EnPU0-tdSeXOdMdSQ",
          "title": "Introducing Sitecore Composable DXP Products",
          "description": "Learn about the powerful features and benefits of Sitecore Composable DXP Products."
        }
      ]
    }
  }
}
choneapi07.png

By switching API keys and endpoints, only published content could be retrieved.

Summary

Looking at the Json data actually retrieved, we were able to confirm that it is possible to retrieve data in the form of drafts and published. In the next article, we will check how to access the data using Postman instead of the IDE.

Tags