Skip to content

Get Started Astro Tutorial — 6. Connecting Content Island with Astro

00:00 / 00:00

6. Connecting Content Island with Astro

We’re now ready to start inserting content from Content Island into our site.

Let’s set up Content Island. What will we need?

  • Install the Content Island API library.
  • Add an environment variable with the API Key for our project in Content Island.

To install the Content Island API client library:

npm install @content-island/api-client --save

We’ll also need to add our API Key as an environment variable.

To do this, go to Content Island, select the project you’re working on—get-started-blog—and under the General tab, you can copy the API Key (field Token).

Token input

Now create a .env file at the root of your project and add the following:

CONTENT_ISLAND_SECRET_TOKEN=92925813362dd0bdc4816da745a90121

From Astro v5 onward, you can type environment variables like this:

./astro.config.mjs

import { defineConfig, envField } from "astro/config";

export default defineConfig({
  env: {
    schema: {
      CONTENT_ISLAND_SECRET_TOKEN: envField.string({
        context: "server",
        access: "secret",
        optional: false,
        default: "INFORM_VALID_TOKEN",
      }),
    },
  },
});

This variable will run on the server when Astro generates the static site.

Now let’s initialize the Content Island client in our project and inject the API key:

./src/lib/client.ts

import { CONTENT_ISLAND_SECRET_TOKEN } from "astro:env/server";
import { createClient } from "@content-island/api-client";

const client = createClient({
  accessToken: CONTENT_ISLAND_SECRET_TOKEN,
});

export default client;

And now everything is ready to start working with Content Island. :)