Call a Third-Party CMS from Your Site

Let’s use an environment variable to store an API key for a third-party content management system (CMS). Then we can implement code that uses that API key to call your CMS from your site. For example, the call can display promotional content managed by your CMS.

For security reasons, the API key calls the CMS from server-side code, not client-side code.

To follow the steps described in this tutorial, you must first complete these prerequisites.

Store custom data in your CMS. For example, a custom homepage title.

  1. To set the environment variable in the current session, open a terminal and run this command:

    • Windows: set CMS_API_KEY=$YOUR_API_KEY_VALUE
    • macOS: export CMS_API_KEY=$YOUR_API_KEY_VALUE
  2. Check if the environment variable is set correctly by running:

    • Windows: echo %CMS_API_KEY%
    • macOS: echo $CMS_API_KEY

    The command returns this output:

    $YOUR_API_KEY_VALUE

  3. To access the value of CMS_API_KEY, use this JavaScript code in your web server:

    const apiKey = process.env.CMS_API_KEY;

    The API key variable is set to this value:

    $YOUR_API_KEY_VALUE

  4. Add this JavaScript code for calling the CMS using an express handler in ssr.js:

  1. To start your web server locally, run:

    npm start

  2. Go to http://localhost:3000/$YOUR_LOCATION_FOR_CMS_DATA to see the data returned by the CMS API.

If the code runs successfully, your custom data is returned by your CMS.

We recommend testing these steps in a non-production environment before deploying your changes to production.

  1. Use cURL to call the projects_target_env_var_partial_update API endpoint and store the API key for your CMS as an environment variable. This deploys your bundle. Wait for the environment to redeploy before using the API key in your code.
  1. Deploy your bundle from your local machine to your chosen environment.