Notifications: Bulk Operations
To add or remove many alerts at once, loop over your addresses or paginate your existing alerts and send one API or SDK request per alert.
Handling large amounts of alerts to notifications can be challenging.
You will find in this article two code snippet examples to retrieve and delete alerts in bulk.
Tatum staff does not troubleshoot code.
Bulk Adding Alerts
To create an alert for multiple addresses at once, you can use an array of addresses and iterate through them. This allows you to create subscriptions for multiple addresses efficiently.
SDK Example
import { TatumSDK, Network, Ethereum } from '@tatumio/tatum';
const tatum = await TatumSDK.init<Ethereum>({ network: Network.ETHEREUM, apiKey: '{YOUR_API_KEY}' });
const addresses = [
'0xF64E82131BE01618487Da5142fc9d289cbb60E9d',
'0x123456789abcdef0123456789abcdef012345678',
'0xabcdef0123456789abcdef0123456789abcdef01',
// Add more addresses as needed
];
const webhookUrl = 'https://<YOUR_WEBHOOK_URL>';
const subscriptions = await Promise.all(addresses.map(address =>
tatum.notification.subscribe.addressEvent({
address: address,
url: webhookUrl
})
));
await tatum.destroy();API Example
import axios from 'axios';
const apiUrl = 'https://api.tatum.io/v4/subscription';
const headers = {
'Content-Type': 'application/json',
'x-api-key': '{YOUR_API_KEY}'
};
const addresses = [
'0xF64E82131BE01618487Da5142fc9d289cbb60E9d',
'0x123456789abcdef0123456789abcdef012345678',
'0xabcdef0123456789abcdef0123456789abcdef01'
];
const webhookUrl = 'https://<YOUR_WEBHOOK_URL>';
const subscriptionPromises = addresses.map(address =>
axios.post(apiUrl, {
type: 'ADDRESS_EVENT',
attr: {
address: address,
chain: 'ETH',
url: webhookUrl
}
}, { headers })
);
const responses = await Promise.all(subscriptionPromises);Bulk Removal of Alerts
To delete an alert from all existing alerts, you can first retrieve all alerts and then delete alert from each one. Because each request to get existing alerts returns a finite number of alerts, we can use an iterative approach to retrieve all.
SDK Example
import { TatumSDK, Network, Ethereum, NotificationSubscription } from '@tatumio/tatum';
const tatum = await TatumSDK.init<Ethereum>({ network: Network.ETHEREUM, apiKey: '{YOUR_API_KEY}' });
async function getAllSubscriptions(): Promise<NotificationSubscription[]> {
let allSubscriptions: NotificationSubscription[] = [];
let offset = 0;
const pageSize = 10;
let hasMore = true;
while (hasMore) {
const response = await tatum.notification.getAll({
pageSize,
offset
});
allSubscriptions = allSubscriptions.concat(response.data);
if (response.data.length < pageSize) {
hasMore = false;
} else {
offset += pageSize;
}
}
return allSubscriptions;
}
// Get all existing subscriptions
const subscriptions = await getAllSubscriptions();
// Unsubscribe from all subscriptions
await Promise.all(subscriptions.map(subscription =>
tatum.notification.unsubscribe(subscription.id)
));
await tatum.destroy();API Example
import axios from 'axios';
const apiUrl = 'https://api.tatum.io/v4/subscription';
const headers = {
'Content-Type': 'application/json',
'x-api-key': '{YOUR_API_KEY}'
};
// Only the fields this example uses
type NotificationSubscription = { id: string };
async function getAllSubscriptions() {
let allSubscriptions: NotificationSubscription[] = [];
let offset = 0;
const pageSize = 10;
let hasMore = true;
while (hasMore) {
const response = await axios.get(apiUrl, {
headers,
params: {
pageSize,
offset
}
});
const subscriptions: NotificationSubscription[] = response.data;
allSubscriptions = allSubscriptions.concat(subscriptions);
if (subscriptions.length < pageSize) {
hasMore = false;
} else {
offset += pageSize;
}
}
return allSubscriptions;
}
// First, get all subscriptions
const subscriptions = await getAllSubscriptions();
// Then, delete each subscription
const deletePromises = subscriptions.map(subscription =>
axios.delete(`${apiUrl}/${subscription.id}`, { headers })
);
await Promise.all(deletePromises);Updated 8 days ago