# Transaction Webhooks

API calls are always asynchronous. However, when you call an API endpoint, you expect to get a response within a relatively short timeframe. In payment processing, a single call to an endpoint may invoke a chain of other API calls between the target and other payment processors.

Waiting for these calls to complete will require very long-lived requests that will bog down server resources on all the platforms involved. It can also cause timeout errors as certain servers can only hold a request pipeline open for fixed periods.

Popular solutions to this problem include:

* Polling - You continually make requests at intervals for the status of the transaction.
* Webhooks - You provide a URL we will call to update the transaction status once it is resolved.

It is obvious that webhooks provide a more efficient solution.&#x20;

<figure><img src="https://images.ctfassets.net/ee3ypdtck0rk/1UuClqOdPD71XXPcFC8th1/67a75ff8542382ade6fd809671d96b44/long-polling-f6e3a73a589fe25d7c7b622a8487a2e8a27a11f00b22b574abb021fbcd7ac2db.png" alt=""><figcaption><p><strong>Long polling request diagram</strong> (The client continuosly sends requests to the server. This is wasteful.)</p></figcaption></figure>

You can configure your webhook URL on the developer dashboard.&#x20;

{% hint style="info" %}
**NOTE:** Your webhook URL must be reachable on the open internet without authentication. Localhost URLs cannot be used directly as endpoints. However certain services may enable you to link localhost ports to an open address eg **`ngrok`**.
{% endhint %}

{% hint style="warning" %}
**IMPORTANT:** Because your webhook endpoint is exposed to the internet without a security wall, we advise you to always [**verify the webhook event source**](#verify-webhook-event-source).&#x20;
{% endhint %}

Certain requests also allow you to provide a callback URL  that should be called in case of the completion of a transaction. This means that both your callback URL and webhook&#x20;

### Verify webhook event source

Webhook endpoints must be publicly accessible. This means you must verify that the call is from SwitchApp before giving value. One way to do this is to validate the **`x-switchapp-signature`** header sent with every webhook event. The value of this header is a `HMAC SHA512` signature of the event payload signed using your secret key. Below are samples of how to validate the signature:

{% tabs %}
{% tab title="node" %}

```javascript
var crypto = require('crypto');
var secret = process.env.SECRET_KEY;
// Using Express
app.post("/my/webhook/url", function(req, res) {
    //validate event
    const hash = crypto.createHmac('sha512', secret).update(JSON.stringify(req.body)).digest('hex');
    if (hash == req.headers['x-switchapp-signature']) {
    // Retrieve the request's body
    const event = req.body;
    // Do something with event  
    }
    res.send(200);
});
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.switchappgo.com/payments/transactions/transaction-webhooks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
