Skip to main content

Build Your First Integration

This tutorial walks you through some basic concepts of integration development. You will:

  • Fetch data from Commerce.
  • Learn how data flows between steps of an integration.
  • Send the data to another third-party.

Integration Overview

The integration you build here retrieves an access token to grant you access to the Commerce API, verifies the access token, and logs the results.

Prerequisites

Gather Elastic Path Composable Commerce Data

You will need to create a connection using Elastic Path keys and authorization URL. See Application Keys. You can find this information in SYSTEM > Application Keys in Commerce Manager when logged in as a user with Seller Admin privileges.

OptionDescription
API Base URLThis is the API URL.
Client IDYour Store's Client ID.
Client SecretYour Store's Client Secret.

Create a New Integration

  1. In Commerce Manager, go to COMPOSER > Builder.
  2. Select + Add integration. The Configure new integration page displays.
  3. From Create new integration, select Quickstart > Build an integration from the ground up. The Details page is displayed.
  4. From Create new integration details page, provide the Name, enter a name My First Integration for your integration.
  5. select Webhook for the trigger and click Create trigger.

Review the Integration Trigger

Most integrations are either scheduled events or webhooks triggered by an Elastic Path observable event. For this tutorial, we are using the Webhook Trigger.

To see the details of the webhook step, click the Trigger step tile to display the drawer details. By default, the following settings are set:

  1. Response Type is set to Asynchronous.
  2. You can optionally set Response Status Code to 200 OK.
  3. You can optionally set Response Content Type to application/json.

Add Your First Step

  1. Click + under the Webhook step to add a new step.

  2. From Add a step, select the Code component. The Code component displays.

  3. Select the Code Block action. Author and run your own code action. The Code Block action is added to the flow.

  4. Click the step tile to display the drawer details.

  5. Select the Details tab in the details drawer and enter a name for the step, for example, Get EPCC Client Token.

  6. Click the Configure tab.

  7. Click Edit, enter the following:

        const params =  new  URLSearchParams();
    module.exports = async ({ logger, configVars }, stepResults) => {

    let base_url = "{{paste the url collected in gather step above}}"
    let client_id = "{{paste the client id collected in gather step above}}"
    let client_secret = "{{paste the client secret collected in gather step above}}"
    params.append('client_id', client_id);
    params.append('client_secret', client_secret);
    params.append('grant_type', 'client_credentials');
    var response = await fetch(base_url + '/oauth/access_token',
    {method: 'POST',body: params});

    var data = await response.json();

    return { data };
    };
  8. Click the Check mark on the upper right corner of the embedded development dialog.

  9. Click Save.

  10. To test your code, click Run in the lower left to trigger manual execution of your integration. It should say Running until each step completed.

  11. Select the Get EPCC Client Token step in the Steps panel to see the results of the code in the right Output panel.

This example takes static code and executes a POST to the Commerce API and retrieves the access token and makes that JSON response available to subsequent steps.

(Optional) Get External Data

The following steps will generate a number between 1 and 325, and retrieve the details of that Beer from Sample APIs open API, and and log the data.

  1. Click + under Success Log message step to add a step to generate a random number between 1 and 325.

  2. Type Math in the search box and select the Math component.

  3. Search for Random in the actions search box and select Random Integer.

  4. In the Min input box type 1 and in the Max input box type 325.

  5. Click the + under "Random Integer" and search for HTTP.

  6. Select the HTTP component and then choose Get Request.

  7. Click In the URL box, click the Template tab and type https://api.sampleapis.com/beers/stouts/.

  8. To the right of the input box, click the + symbol to add a template field.

  9. Click the Reference tab.

    The Reference Step should be Random Integer, and then in Value Preview double click the word results.

  10. Finally, click the + below the Get Request step, seach for Log message again, and choose Write Message and for the "Log Message" click the Reference tab again and select the results of the Get Request step.

Test Your Integration

Click Save in the upper right corner and then Run in the lower left once save has completed. This will open the Test History dialog and show the results of the integration test.