Getting Delivery & Preview Access Token

Hi,

i’m trying to get Delivery & Preview Access Token from a space.

If I use contentful-management.js i’m able to get only the Delivery Access Token with space.getApiKeys(). In the response I found also an object called preview_api_key with an id inside. If I make a second call with this id the response is a 404.

I’ve checked the webapp to understend the correct way, and the webapp calls

/spaces/${spaceId}/preview_api_keys/${id}

This endpoint is not present in the official documentation or in the js SDK documentation.

Can you explain me how te reach the Preview Access Token?

Vincenzo

In your code, when you are creating the client, please add:

host: 'preview.contentful.com',

By default, you will be hitting the Delivery API endpoint (cdn.contentful.com) but when you designate the preview host, you will be hitting our Preview API with the preview api key.

Hi Chrisine,

I’m not using Delivery API directly, instead I’m using Contentful Management API to retrive Delivery and Preview token.

V

Is anybody there? …

Hi @vmilone,
Unfortunately, this is not support through the API currently.

Best,
Khaled

I ran into a situation where I needed to create ephemeral Preview APIs keys programatically. As of today, since version v5.x, the Contentful Management API has a few methods to managing Preview API keys.

A Bit of Context

In our team, each developer has their own Contentful environment. We wanted to apply migrations in a similar way the Rails or Phoenix do. Our migration directory looks like this:

➜  tree cms/migrations
cms/migrations
├── 000-create-migrations.ts
└── 001-create-top-level-sections.ts

0 directories, 2 files

We applied them with a custom task: yarn cms:migrate. We are using a ContentType (Migrations) to keep track of the migrations that have been applied. We want entries in Migrations to stay as drafts. Therefore we are forced to use the Preview API to fetch the migrations that have been applied.

We need to get to the Preview API Key access token.

Programatically managing Preview API Keys

Here are the steps required to create and access a Preview API Key:

  1. Create a Content Delivery API Key, bound to your environment.
  2. Fetch the API Key details using your newly created Content Delivery Key.
  3. Create a client bound to the preview environment.
  4. If you, like us, are into ephemeral keys, delete the Content Delivery API key after using it.

Here’s a code snippet using Typescript:

const contentDeliveryApiKey = await space.createApiKey({
    name: `${getEnvironmentName()}_key`,
    environments: [
      {
        sys: {
          type: 'Link',
          linkType: 'Environment',
          id: getEnvironmentName(),
        },
      },
    ],
  });

 // Although the PreviewApiKey does not expose it, 
 // previewApiKey.accessToken is present in the payload
 const previewApiKey = (await space.getPreviewApiKey(
    contentDeliveryApiKey.preview_api_key.sys.id
  )) as PreviewApiKey & { accessToken: string };

  const client = createClient({
    environment: getLocalEnvName(),
    space: process.env.CONTENTFUL_SPACE as string,
    accessToken: previewApiKey.accessToken,
    host: 'preview.contentful.com',
  });
 
// use client as usual
// delete your Api Key after using it
contentDeliveryApiKey.delete()