Use the SendPulse API in flows

SendPulse's chatbot builder includes the API request element, which helps send and receive data from external and internal systems. This extends your options beyond standard scenarios, including creating new objects or sending messages through the API.

In this article, we will talk about how to authorize and send requests to SendPulse through the API.

Authorize requests

Authorize your requests to confirm your identity and allow your chatbot to access SendPulse features through the API.

Copy your SendPulse account credentials

Go to Settings > API. Copy your ID and Secret. You’ll use them to get a token and create chatbot variables.

Create chatbot variables

Create global variables in your chatbot to save your authorization data.

To add a global variable, go to your chatbot > Settings > Bot variables, and click Create variable.

Add the following variables to be able to get a token:

$access_token String Stores the current access token returned by the SendPulse API after authorization. Use it in the Authorization header of your requests.

In the Value field, enter 0 or -.

$access_token_expiration DateTime Stores the date and time when the current token becomes invalid.

In the Value field, enter 0 or -.

$SP_API_ID String Used to get a token.

In the Value field, enter the client_id you copied in your SendPulse account.

$SP_API_SECRET String Used with client_id for authorization.

In the Value field, enter the client_secret you copied in your SendPulse account.

Get an access token

Every request to the SendPulse API requires an access token or key. You can get this token through the API request element in your chatbot flow.

Drag the API request element to the workspace and select the POST request type to send it to https://api.sendpulse.com/oauth/access_token.

In the Request body field, add JSON:

{
   "grant_type":"client_credentials",
   "client_id":"{{$SP_API_ID}}",
   "client_secret":"{{$SP_API_SECRET}}"
}

Read more about authorization in the SendPulse Service REST API.

Click Test Request. If your request is successful, you will receive a response body with data you must save.

Tokens are valid for 1 hour and don't require a new request for each flow run.

Save the token to your chatbot variables

Use the Action: Set variable element to save your token. Select the $access_token global variable, and add the {{$['access_token']}} value from the previous element as JSONPath.

Follow the same steps for the $access_token_expiration global variable. Since your token is valid for 1 hour, you can use this variable before each request to decide if you need a new token or can use the existing one. Update the value accordingly.

In Set variable value, select the Insert variable checkbox and select Run date. Then, select the Add value offset checkbox and set + 59 minutes.

Use the SendPulse API

Once you receive and save the token to your chatbot, you can use it to make requests to the SendPulse API. Each new API request must include an Authorization header with your token.

You can find all available request methods in the SendPulse Service REST API.

Add the API request element to the workspace and configure it.

Method Choose a method based on your goal. For example, use POST to send a message to Telegram and GET to retrieve information. Then, add your method URL.
Header Add Authorization with the Bearer {{$access_token}} value.

The token will be automatically retrieved from the variable.

At the start of your flow, add the Filter: Variable value element to confirm that your token is not empty and still valid. In the settings, select the two global variables and set the following conditions:

$access_token Select the not empty condition.
$access_token_expiration Select the before condition, select the Insert variables checkbox, and set the Run date variable.

As an example, let’s go over how to send a message to another contact through Telegram. In the API request element, select the POST method and paste this URL: https://api.sendpulse.com/telegram/contacts/send.

Click Add header, then type in Authorization in the first field and Bearer {{$access_token}} in the next one.

Add the request body with the required parameters. Here’s what this can look like:

{  
"contact_id": "string", "message": { "type": "photo", "photo": "https://www.cdn.com/photo.png", "caption": "string" } }

You can also choose a ready-made template in Chatbots > Templates > API.

Rate this article about "Use the SendPulse API in flows"

User Rating: 4 / 5 (7)

Previous

Send SendPulse chatbot events using Conversions API

Next

Create a public chatbot flow preview

Popular in Our Blog

Create your own chatbot today