# Message Webhooks

You can receive real-time information about your Enfonica Cloud SMS usage using webhooks. Webhooks are sent using HTTP(S) POST and can be configured to send when:

- an incoming SMS is received on an SMS-enabled phone number
- the delivery status of an outgoing SMS changes

## Specification

### Request

The body of the webhook request is a JSON serialized object.

The request will have header `Content-Type: application/json; charset=utf-8`.

The request will have header `X-Enfonica-Event` which specifies which
event occurred.

### Security

Enfonica signs every webhook with an HMAC-SHA256 signature included in the
`X-Enfonica-Signature` header. See [Request signing](../platform/request-signing.md)
for details.

### Response

Some events expect a response body. This is specified against each event below.

Your server has 10 seconds to respond. A 2xx response means that your server handled
the request. Enfonica follows a redirect, up to 5 times. Any other response, or no
response in time, is a failure.

After a failure, Enfonica tries the next handler URL, if more than one is set. When
every handler URL has failed, Enfonica tries 8 more times, after delays of 1, 2, 4, 8,
60, 1800, 3600, and 7200 seconds, and then stops. That's more than three hours in total.

Because a slow response is retried, your server can receive the same webhook more than
once.

## Incoming message

Incoming message webhooks are sent when an incoming SMS is received on an SMS-enabled phone number.

### Enabling this webhook using Enfonica Console

To enable the webhook using the [Enfonica Console](https://console.enfonica.com), specify the target URL against an SMS-enabled phone number instance.

### Enabling this webhook using the API

To enable the webhook for an SMS-enabled phone number, specify the target URL against the `incomingMessageHandlerUris` field of the phone number instance (`incoming_message_handler_uris` in gRPC).

To enable the webhook for replies to a specific outgoing SMS, specify `replyUrl` when sending the outgoing SMS (`reply_url` in gRPC).

### Specification

When an incoming message is received, Enfonica sends a webhook to the URL
specified against either:

- the phone number the message was received on; or, if set
- the `replyUrl` set on the most recent message between the two parties.

The request will have `X-Enfonica-Event: INCOMING_MESSAGE`.

The payload of the request will be a JSON serialized instance of the
received [message](../api-reference/rest/v1/projects.messages/overview/). For example:

```json
{
  "name": "projects/example/messages/oprrgs28k9dm5ylgcsqywrrt09j6bg",
  "to": "+61491570006",
  "from": "+61491579212",
  "body": "Hi, this is a test",
  "direction": "INCOMING",
  "segmentCount": 1,
  "price": {
    "units": 0,
    "nanos": 0,
    "currencyCode": "AUD"
  },
  "status": "RECEIVED",
  "createTime": "2021-11-16T23:08:33.813264900Z",
  "encoding": "GSM7"
}
```

A [MessageML](messageml.md) response is expected. Alternatively, you can return
`204 No Content` to indicate the webhook was processed successfully and nothing
further should be done.

## Message status update

Message status update webhooks are sent when the delivery status of an outgoing SMS is updated.

### Enabling this webhook using the API

To enable the webhook for a specific outgoing SMS, specify `statusUpdateUrl` when sending the outgoing SMS (`status_update_url` in gRPC).

### Specification

When a message status changes to `SENT`, `DELIVERED`, `UNDELIVERED` or `FAILED`,
Enfonica sends a webhook to the URL specified against the message in the
`statusUpdateUrl` field.

The request will have `X-Enfonica-Event: MESSAGE_STATUS_UPDATE`.

The payload of the request will be a JSON serialized instance of the
updated [message](../api-reference/rest/v1/projects.messages/overview/). For example:

```json
{
  "name": "projects/example/messages/5kdls7d50pnhpvt4ug9tshbnmfh38f",
  "to": "+61491579212",
  "from": "+61491570006",
  "body": "Sorry that we missed your call. Reply 1 to receive a callback.",
  "validityPeriodSeconds": 432000,
  "direction": "OUTGOING",
  "segmentCount": 1,
  "price": {
    "currencyCode": "AUD",
    "units": 0,
    "nanos": 55000000
  },
  "status": "DELIVERED",
  "createTime": "2021-11-16T23:31:56.724983Z",
  "sendTime": "2021-11-16T23:31:56.752067Z",
  "deliverTime": "2021-11-16T23:31:59.454562100Z",
  "encoding": "GSM7"
}
```

No response body is expected.
