DMDS API REST v1 Examples with nodejs

In this tutorial, we will show different examples of how to call the DMDS (Data-Mining and Delivery Services) REST API using JavaScript code with the request library in nodejs. Always keep in mind which method (GET, POST, DELETE, etc.) is being used for each call and how we make the calls, focusing on the URI part.

Returns contact information

In this example, we will explain how to return the groups, events, and fields of a specific contact. In the call, within the URL, you need to specify the email of the user that already exists in the API database. This way, all the information of a contact, such as their name, surname, etc., will be presented to us. Additionally, it will return the groups that the contact belongs to and all their events.

var request = require('request');
request.get({
    headers: {'content-type' : 'application/x-www-form-urlencoded',"Authorization":"xxxxxx"},
    url:     'https://api-dmds-host/v1/contacto/email',
    form:    {}
}, function(error, response, body){
    console.log(body);
});

List invalid contacts

This part of the code will show us all the users that have been invalidated.

var request = require('request');
request.get({
   headers: {'content-type' :'application/x-www-form-urlencoded',"Authorization":"xxxxx"},
   url:'https://api-dmds-host/v1/contactos/invalidos/',
   form: {}
}, function(error, response, body){
  console.log(body);
});

Create or update a contact

With the following part of the code, you can create a new contact or update an existing one. The first variable defined is the email, which acts as a primary key in the database. When updating a contact, we will always rely on their email since it is unique. You can change the group a contact belongs to by adding another existing group name in the groups section. You can also change their name, surname, etc., for all the fields you want to modify.

var request = require('request');
request.post({
   headers: {'content-type' :'application/x-www-form-urlencoded',"Authorization":"xxxxxxxxxxx"},
   url:'https://api-dmds-host/v1/contacto/',
   form: {email:'xxxxxx@xxxx.com',grupos:'xxxxx',nombre:'xxxxxx',apellido:'xxxx',sexo:'x',edad:'xx',invalido:'0'}
}, function(error, response, body){
   console.log(body);
});

Invalidate a contact

In the same update of a contact, you can invalidate or validate a contact. By default, the invalid variable is always set to zero. But if a user wants to unsubscribe or be invalidated, setting this variable to one (invalid: 1) allows us to mark this contact as invalid.

var request = require('request');
request.post({
   headers: {'content-type' :'application/x-www-form-urlencoded',"Authorization":"xxxxxxxxxxx"},
   url:'https://api-dmds-host/v1/contacto/',
   form: {email:'xxxxxx@xxxx.com',grupos:'xxxxx',nombre:'xxxxxx',apellido:'xxxx',sexo:'x',edad:'xx',invalido:'1'}
}, function(error, response, body){
  console.log(body);
});

Send an email

To send an email, we will differentiate the types we want to send. First, there are two different calls for sending an email to a single recipient and another for sending to multiple recipients. For that, the change is very basic: replacing contact with contacts. Two other calls differ between sending HTML inline or via URI. Generic call

var request = require('request');
request.post({
   headers: {'content-type' :'application/x-www-form-urlencoded',"Authorization":"xxxxx"},
   url:'https://api-dmds-host/v1/envio/enviar/',
   form:'{"campana_id":"xxxxxxxxxxx","email":true,"contacto":{"email":"xxxxxxxx@xxxxxxx.com","nombre":"xxxx","apellido":"xxxxx"}}'
}, function(error, response, body){
console.log(body);
});

Send an email to a single recipient with HTML URI

To perform this send, some fields need to be defined, such as: the email field must always be true, as this allows us to send the email. If this value is false, messages cannot be sent. Other values to define are the campaign ID and the recipient’s email. Other values such as name, surname, and subject are optional. If you want to send HTML via URI, this is added in the html_url field. Remember to set your authorization value in the headers section.

var request = require('request');
request.post({
   headers: {'content-type' :'application/x-www-form-urlencoded',"Authorization":"xxxxx"},
   url:'https://api-dmds-host/v1/envio/send_one_uri/',
  form: {"campana_id":"xxxxx","email":true,"contacto":{"email":"xxxx","nombre":"xxxx","apellido":"xxx"}, "html_url":"http://xxxxxx.html"}
}, function(error, response, body){
console.log(body);
});

Send an email to a single recipient with HTML inline

The difference from the previous call is in how you define the HTML within the code. There are two ways to send an email with HTML inline. One is to define the HTML code within the script:

var request = require('request');
request.post({
   headers: {'content-type' :'application/x-www-form-urlencoded',"Authorization":"xxxxxx"},
   url:'https://api-dmds-host/v1/envio/send_one_inline/',
   form: {"campana_id":"xxxxxx","email":true,"contacto":{"email":"xxxxx","nombre":"xxxxx","apellido":"xxx"}, "html":"<html><head><title>Page Title</title></head><body><h1>This is a Heading</h1><p>This is a paragraph.</p></body></html>"}
}, function(error, response, body){
  console.log(body);
});

Send an email to more than one recipient with HTML inline

var request = require('request');
request.post({
   headers: {'content-type' :'application/x-www-form-urlencoded',"Authorization":"xxxxxx"},
   url:'https://api-dmds-host/v1/envio/send_one_inline/',
   form: {"campana_id":"xxxxxx","email":true,"contactos":{"email":"xxxxx","nombre":"xxxxx","apellido":"xxx"}, "html":"<html><head><title>Page Title</title></head><body><h1>This is a Heading</h1><p>This is a paragraph.</p></body></html>"}
},function(error, response, body) {
   console.log(body);
});

List global variables and their values

There are a series of global variables defined in the API. To see what these variables are and their values, we can look at the following code.

var request = require('request');
request.get({
   headers: {'content-type' :'application/x-www-form-urlencoded',"Authorization":"xxxxxx"},
   url:'https://api-dmds-host/v1/global/',
   form: {}
}, function(error, response, body){
   console.log(body);
});

Create global variables

We can always create global variables. For the creation, we will use the POST method, where we pass a variable indicating the name of the global variable.

var request = require('request');
request.post({
   headers: {'content-type' :'application/x-www-form-urlencoded',"Authorization":"APIKEYxxxxx"},
   url:'https://api-dmds-host/v1/global/',
   form: {"nombre":"loreispum", "valor":"hola"}
}, function(error, response, body){
  console.log(body);
});

List campaigns

With the same structure as listing invalid contacts or global variables, we can list all existing campaigns. For this, the GET method is used.

var request = require('request');
request.get({
   headers: {'content-type' :'application/x-www-form-urlencoded',"Authorization":"xxxxxx"},
   url:'https://api-dmds-host/v1/campania/',
   form: {}
}, function(error, response, body){
console.log(body);
});

Create a group

As we have seen before, when we create or modify a user, we can assign them to a group. To assign a group, we need to see if it exists. If it doesn’t, we can create it as follows.

var request = require('request');
request.post({
  headers: {'content-type' :'application/x-www-form-urlencoded',"Authorization":"APIKEYxxxxx"},
  url:'https://api-dmds-host/v1/grupo/',
  form: {"nombre":"grupo_upf"}
}, function(error, response, body){
console.log(body);
});

List filters

In the API system, different filters can be created to help specify the sends. To see this list of existing filters, follow the code mentioned below.

var request = require('request');
request.get({
   headers: {'content-type' :'application/x-www-form-urlencoded',"Authorization":"APIKEYxxxxx"},
   url:'https://api-dmds-host/v1/filtros/',
   form: {}
}, function(error, response, body){
console.log(body);
});

Última actualización el 2024-08-21