Webhook quickstart
This guide covers setting up a basic flow to receive webhook notifications from the API using a server.
The result is a server running on Express.js that listens for incoming ApprovedPayment events and queries the API for more information about each transaction.
Dependencies
Before you begin, make sure you have the following installed on your machine:
Set up the server
Run these commands in your terminal to configure the webhook example server on your local machine:
Write the server code
The interactive walkthrough displays code examples alongside step-by-step explanations.
/// Import dependencies
Import the required package to create an Express server.
/// Initialize Express app
Create an Express application and configure it to parse JSON request bodies from incoming webhooks using Express’s built-in JSON parser.
/// Define API credentials
Set your Payabli API key and environment. Replace YOUR_API_KEY with your actual API key from your Payabli account. Set API_BASE_URL to the sandbox environment’s base URL.
/// Create webhook endpoint
Define a POST /webhook endpoint to receive webhook notifications from Payabli. The endpoint must return a success response to Payabli’s API.
/// Extract event details
Parse the webhook payload to extract the event type and transaction ID. These values identify what event occurred and which transaction triggered the event.
/// Query API for transaction details
Use the transaction ID to query the Payabli API for the transaction’s details. Set the requestToken header to your API key.
/// Configure the server
Configure the server to listen on port 3000.
Expose the server
To receive webhooks from the API, your server must be publicly accessible from the internet. You can expose your local server to the public internet using a tunneling tool. There are many tunneling tools available for testing environments. This section covers exposing your local server using two common tunneling tools:
- localhost.run (Requires SSH installed)
- ngrok (Requires a free account)
Choosing a tool
You can use either localhost.run or ngrok in this guide to expose your server.
localhost.run doesn’t require an account to set up and is faster to get started with. localhost.run requires the command line utility SSH to be installed on your machine. Most Unix-based operating systems (Linux, macOS) have SSH pre-installed.
ngrok requires users to create a free account and install the ngrok software. ngrok has more features for managing tunnels and inspecting traffic. If you want to inspect webhook traffic thoroughly, we recommend using ngrok.
Select the tool you want to use and follow the instructions to publicly expose your local server:
localhost.run
ngrok
Create a free ngrok account
Go to ngrok.com and sign up for a free account. If you already have ngrok set up, skip to step 5.
Download your auth token
After logging in to your ngrok account, go to the dashboard to find your authentication token.
Create a webhook notification
In order to receive webhook notifications, you must first create a notification via the API.
Let’s create a notification that sends a webhook for the ApprovedPayment event.
Call the POST /Notification endpoint to create a webhook notification.
Set the target field to the public URL you created in the previous step, with /webhook appended at the end.
See Create notification for more information about the POST /Notification/ endpoint.
See ApprovedPayment for more information about the ApprovedPayment event.
Test the webhook notification
You’ve set up your server, exposed it to the internet, and created a webhook notification pointing to it. Let’s create a test file that processes a payment in order to trigger the webhook notification.
Write the test code
Use the interactive walkthrough to process a test payment in order to trigger your webhook notification.
The interactive walkthrough displays code examples alongside step-by-step explanations.
/// Define API credentials
Set your Payabli API key, entrypoint, and the sandbox API base URL. Replace YOUR_API_KEY and YOUR_ENTRYPOINT with your actual credentials from your Payabli account.
/// Build the payment request
Create a request object with the paymentDetails, paymentMethod, and customerData objects. This example uses a test card number.
/// Make the API request
Use the fetch API to send a POST request to the API’s /MoneyIn/getpaid endpoint. Include the API key in the requestToken header and include the entrypoint in the entryPoint field.
/// Handle the response
Parse the JSON response and display the payment result. Check if the transaction was successful and log the reference ID.
/// Add error handling
Add error handling to catch any network errors or API failures during the payment process.
Next steps
Congrats! You’ve built a basic webhook integration that includes:
- A webhook notification for the
ApprovedPaymentevent - A server that listens for the
ApprovedPaymentevent and fetches details about the payment
Try creating new webhook notifications for different event types and triggering them with API calls. All webhook integrations follow the same structure as the one you built in this guide. See the Webhook notification response reference for more information about the different event types you can create webhook notifications for. Contact your Payabli solutions engineer for assistance with webhook integrations in your projects.
Related resources
See these related resources to help you get the most out of Payabli.
Prerequisites
- Notifications and reports overview - Get automated reports and notifications for key events