What are webhooks

Webhooks are user-defined HTTP callback that notifies you about any action that has taken place.

YCloud uses webhooks to push real-time notifications to your applications. By configuring webhooks and subscribing to specific events, you can receive event notifications and use these notifications to execute actions in your backend systems.

Steps to receive webhooks

You can start receiving event notifications in your applications using the steps in this section:

  1. Identify the events to monitor and the event payloads to parse.
  2. Create a webhook endpoint.
  3. Handle requests from YCloud by parsing each event and returning 2xx response status codes.
  4. Secure your webhooks (recommended)

Step 1: Identify the events to monitor

Each event corresponds to a certain set of actions that can happen to your account. For example, if you subscribe to the sms.message.updated event you'll receive detailed payloads every time an SMS message is updated with a new status.

For a complete list of available webhook events and their payloads, see Webhooks events & payloads.

Step 2: Create a webhook endpoint

You can use the Webhook Endpoints APIs to manage webhooks.

Step 3: Handle requests from YCloud

Check event objects

Each event is structured as an event object with common properties: id, type, apiVersion, and createTime, and also contains properties unique to the event depending on type.

Here is an example of an event with type = sms.message.updated:

{
  "id": "evt_djeIQXaQPQyUcRFi",
  "type": "sms.message.updated",
  "apiVersion": "v2",
  "createTime": "2022-03-01T12:00:00.000Z",
  "sms": {
    "id": "string",
    "to": "+447901614024",
    "text": "Your verification code is 123456.",
    "senderId": "YCloud",
    "regionCode": "GB",
    "totalSegments": 1,
    "totalPrice": 0.0085,
    "currency": "USD",
    "status": "delivered",
    "createTime": "2022-03-01T12:00:00.000Z",
    "updateTime": "2022-03-01T12:00:00.000Z"
  }
}

Return a 2xx response

Your endpoint must quickly return a successful HTTP status (e.g., 200) before any complex logic that could cause a timeout.

Build-in retries

If YCloud doesn't quickly receive a 2xx response for an event, we'll retry several times in the next few hours.

Step 4: Secure your webhooks (recommended)

YCloud can sign the webhook events by including a signature in the HTTP header YCloud-Signature. This allows you to verify that the events were sent by YCloud, not by a third party.

Before you can verify signatures, you need to retrieve your endpoint's secret by Retrieve a webhook endpoint API.

The YCloud-Signature header contains a Unix Timestamp and a signature generated by HMAC with SHA-256.

YCloud-Signature: t=1654084800,s=8eb70f2acb056c2119acbee8fdd98a889021d9c268bc9ad248a4182c40e31119

Verify the signature by the following steps:

Step 1: Extract the timestamp and signatures from the header

Split the header, using the , character as the separator, to get a list of elements. Next, split each element, using the = character as the separator, to get a prefix and value pair.

The value for the prefix t corresponds to the timestamp and s corresponds to the signature.

Step 2: Prepare the signed payload string

The signed payload string is created by concatenating:

  • The timestamp (as a string)
  • The character .
  • The actual JSON payload (that is, the request body)

For example:

1654084800.{"id":"evt_djeIQXaQPQyUcRFi","type":"sms.message.updated","apiVersion":"v2","createTime":"2022-03-01T12:00:00.000Z","sms":{"id":"string","to":"+447901614024","text":"Your verification code is 123456.","senderId":"YCloud","regionCode":"GB","totalSegments":1,"totalPrice":0.0085,"currency":"USD","status":"delivered","createTime":"2022-03-01T12:00:00.000Z","updateTime":"2022-03-01T12:00:00.000Z"}}

Step3: Determine the expected signature

Compute an HMAC with the SHA256 hash function. Use the endpoint’s signing secret as the key, and use the signed payload string as the message.

const crypto = require('crypto');

const toBeSignedPayload = timestamp + '.' + body
const expected = crypto.createHmac('sha256', secret)
.update(toBeSignedPayload)
.digest("hex");
using System;
using System.Security.Cryptography;
using System.Text;

class MainClass {
  public static void Main (string[] args) {
    string toBeSignedPayload = timestamp + "." + body;

    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] secretBytes = encoding.GetBytes(secret);
    byte[] messageBytes = encoding.GetBytes(toBeSignedPayload);
    HMACSHA256 cryptographer = new HMACSHA256(secretBytes);

    byte[] expectedBytes = cryptographer.ComputeHash(messageBytes);

    string expected = BitConverter.ToString(expectedBytes).Replace("-", "").ToLower();

    Console.WriteLine(expected);

  }
}

Step 4: Compare the signatures

Compare the signature in the header to the expected signature. For an equality match, compute the difference between the current timestamp and the received timestamp, then decide if the difference is within your tolerance.