Skip to main content

Using Webhooks

Webhooks let AudioSpliter push notifications to your server when jobs complete or fail, eliminating the need for polling.

Step 1: Create a Webhook Endpoint

Set up an HTTPS endpoint on your server that accepts POST requests:

Express.js

const express = require('express');
const crypto = require('crypto');
const app = express();

app.post('/webhooks/audiospliter', express.json(), (req, res) => {
// Verify signature
const signature = req.headers['x-audiospliter-signature'];
const expected = crypto
.createHmac('sha256', process.env.WEBHOOK_SECRET)
.update(JSON.stringify(req.body))
.digest('hex');

if (!crypto.timingSafeEqual(Buffer.from(signature, 'hex'), Buffer.from(expected, 'hex'))) {
return res.status(401).json({ error: 'Invalid signature' });
}

// Handle the event
const { event, jobId, data } = req.body;

switch (event) {
case 'job.completed':
console.log(`Job ${jobId} completed with ${data.segments.length} segments`);
// Download segments, update your database, etc.
break;
case 'job.failed':
console.error(`Job ${jobId} failed: ${data.error.message}`);
break;
}

res.status(200).json({ received: true });
});

Flask (Python)

from flask import Flask, request, jsonify
import hmac, hashlib, os

app = Flask(__name__)

@app.route('/webhooks/audiospliter', methods=['POST'])
def webhook():
signature = request.headers.get('X-AudioSpliter-Signature', '')
expected = hmac.new(
os.environ['WEBHOOK_SECRET'].encode(),
request.get_data(),
hashlib.sha256,
).hexdigest()

if not hmac.compare_digest(expected, signature):
return jsonify({'error': 'Invalid signature'}), 401

data = request.get_json()

if data['event'] == 'job.completed':
print(f"Job {data['jobId']} done: {len(data['data']['segments'])} segments")
elif data['event'] == 'job.failed':
print(f"Job {data['jobId']} failed: {data['data']['error']['message']}")

return jsonify({'received': True}), 200

Step 2: Include the Webhook URL in Your Job

curl -X POST https://api.audiospliter.com/api/v1/splits \
-H "X-API-Key: as_live_abc123" \
-H "Content-Type: application/json" \
-d '{
"sourceUrl": "https://example.com/audio.mp3",
"splitMode": "duration",
"segmentDuration": 600,
"webhookUrl": "https://your-app.com/webhooks/audiospliter"
}'

Step 3: Process the Callback

When the job completes, AudioSpliter sends a POST request to your webhook URL with the job result. Download segments immediately or store the URLs for later use.

Testing Locally

Use a tunnel service like ngrok to expose your local endpoint:

ngrok http 3000
# Use the generated HTTPS URL as your webhookUrl

See Also