Create and publish a consent notice

This tutorial will walk you through the steps necessary to create a notice, modify its configuration, and publish it.

Note: If you're looking to create a multi-regulation configuration please see here instead.

To create a consent notice, send a POST /widgets/notices request with the name of the notice to create and the ID of the organization that the notice belongs to:

const bent = require("bent");

const organizationId = "<organizationId>";
const token = "<token>"; // Get a token by authenticating in the API (https://developers.didomi.io/api/introduction/authentication)

const client = {
  post: bent(
    "POST",
    "https://api.didomi.io/v1",
    "json",
    {
      Authorization: `Bearer ${token}`,
    },
    201
  ),
};

(async () => {
  console.log(
    `Creating a notice for organization ${organizationId} ...`
  );

  const notice = await client.post("/widgets/notices", {
    organization_id: organizationId,
    name: "My consent notice",
  });

  console.log(`Notice with ID ${notice.id} created!`);
})();

The API will return a response similar to:

{
    "name": "My consent notice",
    "archived_at": null,
    "organization_id": "didomi",
    "id": "7wmRYUH6",
    "created_at": "2021-03-27T08:38:10.831Z",
    "updated_at": "2021-03-27T08:38:10.831Z",
    "version": 0
}

Modify the notice configuration

The API automatically created a draft configuration when a notice was created. To update the notice configuration, you must first get the ID of the draft configuration and then modify that draft configuration.

Get the ID of the draft configuration

To get the ID of the draft configuration, get the list of all configurations:

const bent = require("bent");

const organizationId = "<organizationId>";
const noticeId = "<noticeId>";
const token = "<token>"; // Get a token by authenticating in the API (https://developers.didomi.io/api/introduction/authentication)

const client = {
  get: bent("GET", "https://api.didomi.io/v1", "json", {
    Authorization: `Bearer ${token}`,
  }),
  patch: bent("PATCH", "https://api.didomi.io/v1", "json", {
    Authorization: `Bearer ${token}`,
  }),
  post: bent(
    "POST",
    "https://api.didomi.io/v1",
    "json",
    {
      Authorization: `Bearer ${token}`,
    },
    201
  ),
};

(async () => {
  console.log(
    `Querying the organization: ${organizationId} and the notice: ${noticeId}`
  );

  // Get the notice configs for the organization and notice
  console.log("Getting the latest draft config ...");
  const request = await client.get(
    `/widgets/notices/configs?organization_id=${organizationId}&notice_id=${noticeId}`
  );
  const noticeConfigs = request.data;

  // Filter the notice configs to get the draft config.
  const draftConfig = noticeConfigs.filter((e) => e.deployed_at === null).pop();
  const draftConfigId = draftConfig.id;
})();

The configuration is called "draft" because its deployed_at field is null which indicates that this configuration has not been published before and can be modified.

As we are working with a newly created notice in this tutorial, we can assume that it has a single associated configuration and that it is a draft. If you are working with an existing notice that might have been published before and would have multiple configurations, read our documentation to find the current draft documentation.

Modify the draft configuration

To modify the draft configuration, send a PATCH request with the updated configuration:

const bent = require("bent");
const { merge } = require("lodash");

const organizationId = "<organizationId>";
const noticeId = "<noticeId>";
const token = "<token>"; // Get a token by authenticating in the API (https://developers.didomi.io/api/introduction/authentication)

const client = {
  get: bent("GET", "https://api.didomi.io/v1", "json", {
    Authorization: `Bearer ${token}`,
  }),
  patch: bent("PATCH", "https://api.didomi.io/v1", "json", {
    Authorization: `Bearer ${token}`,
  }),
  post: bent(
    "POST",
    "https://api.didomi.io/v1",
    "json",
    {
      Authorization: `Bearer ${token}`,
    },
    201
  ),
};

(async () => {
  console.log(
    `Querying the organization: ${organizationId} and the notice: ${noticeId}`
  );

  // Get the notice configs for the organization and notice
  console.log("Getting the latest draft config ...");
  const request = await client.get(
    `/widgets/notices/configs?organization_id=${organizationId}&notice_id=${noticeId}`
  );
  const noticeConfigs = request.data;

  // Filter the notice configs to get the draft config.
  const draftConfig = noticeConfigs.filter((e) => e.deployed_at === null).pop();
  const draftConfigId = draftConfig.id;

  // Modify the notice configuration
  console.log("Updating the latest draft config ...");
  await client.patch(
    `/widgets/notices/configs/${draftConfigId}`,
    merge(draftConfig, {
      consent_duration: 6, // Change the consent duration for the notice
      consent_duration_unit: "months",
    })
  );
})();

Publish the notice

Now that our notice is configured as we want it to be, we need to publish it so that our modified configuration appears in the actual notice on the website or app.

To publish a notice, we need to create a new deployment for that notice:

const bent = require("bent");
const { merge } = require("lodash");

const organizationId = "<organizationId>";
const noticeId = "<noticeId>";
const token = "<token>"; // Get a token by authenticating in the API (https://developers.didomi.io/api/introduction/authentication)

const client = {
  get: bent("GET", "https://api.didomi.io/v1", "json", {
    Authorization: `Bearer ${token}`,
  }),
  patch: bent("PATCH", "https://api.didomi.io/v1", "json", {
    Authorization: `Bearer ${token}`,
  }),
  post: bent(
    "POST",
    "https://api.didomi.io/v1",
    "json",
    {
      Authorization: `Bearer ${token}`,
    },
    201
  ),
};

(async () => {
  console.log(
    `Querying the organization: ${organizationId} and the notice: ${noticeId}`
  );

  // Get the notice configs for the organization and notice
  console.log("Getting the latest draft config ...");
  const request = await client.get(
    `/widgets/notices/configs?organization_id=${organizationId}&notice_id=${noticeId}`
  );
  const noticeConfigs = request.data;

  // Filter the notice configs to get the draft config.
  const draftConfig = noticeConfigs.filter((e) => e.deployed_at === null).pop();
  const draftConfigId = draftConfig.id;

  // Modify the notice configuration
  console.log("Updating the latest draft config ...");
  await client.patch(
    `/widgets/notices/configs/${draftConfigId}`,
    merge(draftConfig, {
      consent_duration: 6, // Change the consent duration for the notice
      consent_duration_unit: "months",
    })
  );

  // Publish the notice configuration
  console.log("Publishing the draft config ...");
  const response = await client.post(`/widgets/notices/deployments`, {
    notice_id: noticeId,
    organization_id: organizationId,
    production_config_id: draftConfigId,
    message: "Message describing the content of the update to the notice",
  });

  console.log(`Your deployment is a ${response.status}!`);
})();

The production_config_id should be the ID of the draft config you are publishing. message is a text field that can be used to indicate information on what changes are included in your deployment.

The API responds with the status of your deployment:

{
    "message": "Description of changes included in my deployment",
    "notice_id": "7wmRYUH6",
    "production_config_id": "7bKP4CRr",
    "organization_id": "didomi",
    "status": "success",
    "status_message": null,
    "id": "itBtwnxM",
    "created_at": "2021-03-27T08:53:07.467Z",
    "updated_at": "2021-03-27T08:53:07.467Z",
    "version": 0
}

What's next?

The Didomi API allows managing all aspects of consent notices and their configurations. Read more about the entities to learn how to do more complex operations.

Last updated