Files
automatisch/packages/docs/pages/build-integrations/global-variable.md
2022-11-09 22:42:51 +01:00

3.4 KiB

Global Variable

:::info

The build integrations section is best understood when read from beginning to end. To get the most value out of it, start from the first page and read through page by page.

  1. Folder structure
  2. App
  3. Global variable
  4. Auth
  5. Triggers
  6. Actions
  7. Examples

:::

Before handling authentication and building a trigger and an action, it's better to explain the global variable concept in Automatisch. Automatisch provides you the global variable that you need to use with authentication, triggers, action, and basically all the stuff you will build for the integration.

The global variable is represented as $ variable in the codebase, and it's a JSON object that contains the following properties:

$.auth.set

$.auth.set({
  key: 'value',
});

It's used to set the authentication data, and you can use this method with multiple pairs. The data will be stored in the database and can be retrieved later by using $.auth.data property. We use this method when we store the credentials of the third-party service. Note that Automatisch encrypts the data before storing it in the database.

$.auth.data

$.auth.data; // { key: 'value' }

It's used to retrieve the authentication data that we set with $.auth.set(). The data will be retrieved from the database. We use the data property with the key name when we need to get one specific value from the data object.

$.app.baseUrl

$.app.baseUrl; // https://thecatapi.com

It's used to retrieve the base URL of the app that we defined previously. In our example, it returns https://thecatapi.com. We use this property when we need to use the base URL of the third-party service.

$.app.apiBaseUrl

$.app.apiBaseUrl; // https://api.thecatapi.com

It's used to retrieve the API base URL of the app that we defined previously. In our example, it returns https://api.thecatapi.com. We use this property when we need to use the API base URL of the third-party service.

$.http

It's an HTTP client to be used for making HTTP requests. It's a wrapper around the axios library. We use this property when we need to make HTTP requests to the third-party service. The apiBaseUrl field we set up in the app will be used as the base URL for the HTTP requests. For example to search the cat images, we can use the following code:

$.http.get('/v1/images/search?order=DESC', {
  headers: {
    'x-api-key': $.auth.data.apiKey,
  },
});

$.pushTriggerItem

$.pushTriggerItem({
  raw: resourceData,
  meta: {
    id: resourceData.id,
  },
});

It's used to push trigger data to be processed by Automatisch. It must reflect the data that we get from the third-party service. Let's say for search tweets trigger it will be the JSON that represents the tweet object.

$.setActionItem

$.setActionItem({
  raw: resourceData,
});

It's used to set the action data to be processed by Automatisch. For actions, it reflects the response data that we get from the third-party service. Let's say for create tweet action it will be the JSON that represents the response payload we get while creating a tweet.