Creating datastreams in Management API

This article explains how to create datastreams with the Management API.

Prerequisites

Before you complete the procedure in this guide, perform all of the following actions:

Determining a Datastream Type's ID

To determine a Datastream Type's ID, follow these steps:

  1. Create a GET request to the following endpoint:

    https://{{INSTANCE}}/api/datastream-types/
  2. In the HTTP request header, include the parameter Authorization with one of the following values:

    • Token {{KEY}} if you use a key generated with user credentials in Management API.

    • Bearer {{KEY}} if you use a key generated in the Adverity user interface.

  3. Send the request.

As a result, you obtain the available Datastream Types and their IDs in the response.

If you already know the Datastream Type's API name, create a GET request to the following endpoint in step 1.

https://{{INSTANCE}}/api/datastream-types/?search={{DATASTREAM_TYPE_NAME}}

You can also use this method to search for part of the data source name. For example, using ?search=google returns all Datastream Types whose name contains the word google.

If you use this method, the JSON response only includes the Datastream Type you specify.

Import the request example as raw text to your HTTP client (such as Postman). The cURL request example is the following:

curl --location --request GET 'https://{{INSTANCE}}/api/datastream-types/' \
		--header 'Authorization: Token {{KEY}}'

Example of JSON response:

{
    "count": XYZ,
    "next": "https://{{INSTANCE}}/api/datastream-types/?page=XZ",
    "previous": null,
    "results": [
        {
            "id": {{DATASTREAM_TYPE}},
            "name": "DATASTREAM_TYPE_NAME",
            "slug": "DATASTREAM_AUTHORIZATION",
            "url": "https://{{INSTANCE}}/api/datastream-types/{{DATASTREAM_TYPE}}/",
            "datastreams": "https://{{INSTANCE}}/api/datastream-types/{{DATASTREAM_TYPE}}/datastreams/",
            "create_url": "https://{{INSTANCE}}/core/apidatastream/add/?ct_id={{DATASTREAM_TYPE}}",
            "logo_url": "https://{{INSTANCE}}/static/images/api/logo.svg"
        }
    ]
}

The value in an id parameter of the response is a Datastream Type's ID.

Determining Datastream Type options

To determine the options you can configure for a Datastream Type, follow these steps:

  1. Create an OPTIONS request to the following endpoint:

    https://{{INSTANCE}}/api/datastream-types/{{DATASTREAM_TYPE}}/datastreams/

    Replace {{DATASTREAM_TYPE}} with the Datastream Type's ID which you acquired in the previous section.

  2. In the HTTP request header, include the parameter Authorization with one of the following values:

    • Token {{KEY}} if you use a key generated with user credentials in Management API.

    • Bearer {{KEY}} if you use a key generated in the Adverity user interface.

  3. Send the request.

As a result, you obtain the list of options you can configure for the Datastream Type.

Import the request example as raw text to your HTTP client (such as Postman). The cURL request example is the following:

curl --location --request OPTIONS 'https://{{INSTANCE}}/api/datastream-types/{{DATASTREAM_TYPE}}/datastreams/' \
		--header 'Authorization: Token {{KEY}}'

To determine the list of options you must specify for creating the datastream, search for "required": true in the JSON response.

Creating a datastream

To send the request to create a datastream, follow these steps:

  1. Create a POST request to the following endpoint:

    https://{{INSTANCE}}/api/datastream-types/{{DATASTREAM_TYPE}}/datastreams/

    Replace {{DATASTREAM_TYPE}} with the Datastream Type's ID.

  2. In the HTTP request header, include the parameter Authorization with one of the following values:

    • Token {{KEY}} if you use a key generated with user credentials in Management API.

    • Bearer {{KEY}} if you use a key generated in the Adverity user interface.

  3. In the HTTP request header, include the parameter Content-Type with value application/json.

  4. In the HTTP request body, include the options you want to configure for the datastream as parameters.

    The following parameters are always required:

    datatype

    Specify the datastream's status. The value can be either "Live" or "Staging".

    cron_type

    The frequency of scheduled fetches. The value can be "day", for example.

    name

    Provide a name for the datastream.

    stack

    The ID of the workspace where you want to create the datastream.

    The following parameters are also commonly required:

    auth

    The ID of the authorization you use to connect to the data source.

    fields

    The list of fields to collect from the data source.

    report_type

    The report type to collect from the data source.

    time_range_preset

    The time range of scheduled fetches.

  5. Send the request.

As a result, you create the datastream which is disabled by default.

Import the request example as raw text to your HTTP client (such as Postman). The cURL request example is the following:

curl --location --request POST 'https://{{INSTANCE}}/api/datastream-types/{{DATASTREAM_TYPE}}/datastreams/' \
		--header 'Authorization: Token {{KEY}}' \
		--header 'Content-Type: application/json' \
		--data-raw '{
				"datatype": "Staging",
				"cron_type": "day",
				"name": "My new datastream",
				"stack": 123,
				"auth": 123
		}'

The value in the id field of the response is the newly created datastream's ID.

Enabling the datastream

To enable the datastream, follow these steps:

  1. Create a PATCH request to the following endpoint:

    https://{{INSTANCE}}/api/datastreams/{{DATASTREAM_ID}}/

    Replace {{DATASTREAM_ID}} with the newly created datastream's ID.

  2. In the HTTP request header, include the parameter Authorization with one of the following values:

    • Token {{KEY}} if you use a key generated with user credentials in Management API.

    • Bearer {{KEY}} if you use a key generated in the Adverity user interface.

  3. In the HTTP request header, include the parameter Content-Type with value application/json.

  4. In the HTTP request body, include the enabled parameter with the value true.

  5. Send the request.

As a result, you enabled the datastream.

Import the request example as raw text to your HTTP client (such as Postman). The cURL request example is the following:

curl --location --request PATCH 'https://{{INSTANCE}}/api/datastreams/{{DATASTREAM_ID}}/' \
		--header 'Content-Type: application/json' \
		--header 'Authorization: Token {{KEY}}' \
		--data-raw '{"enabled": true}'