Code Examples
Explore practical code examples to help you integrate BillHive into your application.
Create a Customer
const response = await fetch('https://api.billhive.org/v1/customers', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'John Doe',
email: 'john@example.com',
phone: '+1234567890'
})
});
const customer = await response.json();
console.log('Customer created:', customer);Create a Subscription
const response = await fetch('https://api.billhive.org/v1/subscriptions', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
customer_id: 'cust_123',
plan_id: 'plan_456',
billing_cycle: 'monthly'
})
});
const subscription = await response.json();
console.log('Subscription created:', subscription);Fetch Invoices
const response = await fetch('https://api.billhive.org/v1/invoices?customer_id=cust_123', {
method: 'GET',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
}
});
const invoices = await response.json();
console.log('Invoices:', invoices);Handle Webhooks
const express = require('express');
const app = express();
app.post('/webhooks/billhive', express.json(), (req, res) => {
const event = req.body;
switch (event.event) {
case 'invoice.paid':
console.log('Invoice paid:', event.data);
// Handle paid invoice
break;
case 'subscription.cancelled':
console.log('Subscription cancelled:', event.data);
// Handle cancellation
break;
default:
console.log('Unhandled event:', event.event);
}
res.status(200).send('OK');
});
app.listen(3000, () => console.log('Webhook server running on port 3000'));Process a Payment
const response = await fetch('https://api.billhive.org/v1/payments', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
invoice_id: 'inv_789',
payment_method: 'card',
amount: 9999,
currency: 'INR'
})
});
const payment = await response.json();
console.log('Payment processed:', payment);