Webhooks
Webhooks are a powerful tool for receiving real-time notifications about events happening in your PaymentKit account, such as successful payments, subscription updates, or failed charges. By setting up a webhook endpoint, you can automate your backend processes in response to these events.
This document provides a detailed reference for the Webhook Endpoints API. For a higher-level guide on how to implement and secure webhooks, please see the Webhooks guide.
The Webhook Endpoint Object#
A webhook endpoint object contains information about the URL to be notified and the specific events it should listen for.
Attribute | Type | Description |
|---|---|---|
|
| Unique identifier for the webhook endpoint. |
|
|
|
|
| The API version used to render the event data sent to the endpoint. |
|
| The URL of the webhook endpoint. |
|
| An optional description of the webhook endpoint. |
|
| The list of event types that this endpoint is configured to receive. Examples include |
|
| A set of key-value pairs that you can attach to an object. Useful for storing additional information. |
|
| The status of the webhook endpoint, can be either |
|
| Timestamp of when the object was created. |
|
| How the endpoint was created, either |
|
| Timestamp of the last update. |
Create a Webhook Endpoint#
Creates a new webhook endpoint to receive notifications.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The URL where event notifications will be sent via POST request. |
|
| Required. An array of event type strings that you want to subscribe to. |
|
| An optional description for the endpoint. |
|
| The API version for the event data. Defaults to your account's default API version. |
|
| Optional key-value data to store with the endpoint. |
|
| The initial status of the endpoint. Can be |
Returns
Returns the newly created webhook endpoint object.
import payment from '@blocklet/payment-js';
async function createWebhookEndpoint() {
try {
const endpoint = await payment.webhooks.create({
url: 'https://example.com/webhook-receiver',
enabled_events: [
'charge.succeeded',
'customer.subscription.deleted'
],
description: 'Endpoint for production events'
});
console.log('Webhook endpoint created:', endpoint.id);
return endpoint;
} catch (error) {
console.error('Error creating webhook endpoint:', error.message);
}
}
createWebhookEndpoint();Example Response
{
"id": "whep_123456789",
"livemode": false,
"api_version": "2024-01-01",
"url": "https://example.com/webhook-receiver",
"description": "Endpoint for production events",
"enabled_events": [
"charge.succeeded",
"customer.subscription.deleted"
],
"metadata": {},
"status": "enabled",
"created_at": "2024-05-21T10:00:00.000Z",
"created_via": "api",
"updated_at": "2024-05-21T10:00:00.000Z"
}Retrieve a Webhook Endpoint#
Retrieves the details of an existing webhook endpoint by its ID.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The unique identifier of the webhook endpoint to retrieve. |
Returns
Returns the specified webhook endpoint object.
import payment from '@blocklet/payment-js';
async function retrieveWebhookEndpoint(endpointId) {
try {
const endpoint = await payment.webhooks.retrieve(endpointId);
console.log('Retrieved endpoint:', endpoint.url);
return endpoint;
} catch (error) {
console.error(`Error retrieving webhook endpoint ${endpointId}:`, error.message);
}
}
retrieveWebhookEndpoint('whep_123456789');Example Response
{
"id": "whep_123456789",
"livemode": false,
"api_version": "2024-01-01",
"url": "https://example.com/webhook-receiver",
"description": "Endpoint for production events",
"enabled_events": [
"charge.succeeded",
"customer.subscription.deleted"
],
"metadata": {},
"status": "enabled",
"created_at": "2024-05-21T10:00:00.000Z",
"created_via": "api",
"updated_at": "2024-05-21T10:00:00.000Z"
}Update a Webhook Endpoint#
Updates an existing webhook endpoint. You can change its URL, the events it listens for, its description, or its status.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The identifier of the endpoint to update. |
|
| The new URL for the endpoint. |
|
| The new list of event types to subscribe to. This will replace the existing list. |
|
| An updated description. |
|
| The new status, either |
|
| New metadata. This will replace the existing metadata. |
Returns
Returns the updated webhook endpoint object.
import payment from '@blocklet/payment-js';
async function updateWebhookEndpoint(endpointId) {
try {
const endpoint = await payment.webhooks.update(endpointId, {
description: 'Updated endpoint description',
status: 'disabled'
});
console.log('Webhook endpoint updated:', endpoint.id, 'Status:', endpoint.status);
return endpoint;
} catch (error) {
console.error(`Error updating webhook endpoint ${endpointId}:`, error.message);
}
}
updateWebhookEndpoint('whep_123456789');Example Response
{
"id": "whep_123456789",
"livemode": false,
"api_version": "2024-01-01",
"url": "https://example.com/webhook-receiver",
"description": "Updated endpoint description",
"enabled_events": [
"charge.succeeded",
"customer.subscription.deleted"
],
"metadata": {},
"status": "disabled",
"created_at": "2024-05-21T10:00:00.000Z",
"created_via": "api",
"updated_at": "2024-05-21T10:05:00.000Z"
}List all Webhook Endpoints#
Returns a paginated list of all your webhook endpoints.
Parameters
Name | Type | Description |
|---|---|---|
|
| The page number for pagination, starting from 1. |
|
| The number of items to return per page. Defaults to 20. |
| `string | string[]` |
Returns
Returns a paginated object containing a list of webhook endpoints.
Name | Type | Description |
|---|---|---|
|
| The total number of endpoints matching the query. |
|
| An array of webhook endpoint objects. |
import payment from '@blocklet/payment-js';
async function listWebhookEndpoints() {
try {
const endpoints = await payment.webhooks.list({ pageSize: 5 });
console.log(`Found ${endpoints.count} total endpoints.`);
endpoints.list.forEach(endpoint => {
console.log(`- ${endpoint.id} (${endpoint.url})`);
});
return endpoints;
} catch (error) {
console.error('Error listing webhook endpoints:', error.message);
}
}
listWebhookEndpoints();Example Response
{
"count": 1,
"list": [
{
"id": "whep_123456789",
"livemode": false,
"api_version": "2024-01-01",
"url": "https://example.com/webhook-receiver",
"description": "Updated endpoint description",
"enabled_events": [
"charge.succeeded",
"customer.subscription.deleted"
],
"metadata": {},
"status": "disabled",
"created_at": "2024-05-21T10:00:00.000Z",
"created_via": "api",
"updated_at": "2024-05-21T10:05:00.000Z"
}
]
}Delete a Webhook Endpoint#
Permanently deletes a webhook endpoint. This action cannot be undone.
Parameters
Name | Type | Description |
|---|---|---|
|
| Required. The unique identifier of the webhook endpoint to delete. |
Returns
Returns the deleted webhook endpoint object.
import payment from '@blocklet/payment-js';
async function deleteWebhookEndpoint(endpointId) {
try {
const endpoint = await payment.webhooks.del(endpointId);
console.log(`Successfully deleted webhook endpoint ${endpoint.id}.`);
return endpoint;
} catch (error) {
console.error(`Error deleting webhook endpoint ${endpointId}:`, error.message);
}
}
deleteWebhookEndpoint('whep_123456789');Example Response
{
"id": "whep_123456789",
"livemode": false,
"api_version": "2024-01-01",
"url": "https://example.com/webhook-receiver",
"description": "Updated endpoint description",
"enabled_events": [
"charge.succeeded",
"customer.subscription.deleted"
],
"metadata": {},
"status": "disabled",
"created_at": "2024-05-21T10:00:00.000Z",
"created_via": "api",
"updated_at": "2024-05-21T10:05:00.000Z"
}After configuring your webhooks, you might want to manage payment refunds. Proceed to the Refunds API Reference to learn more.