(415) 300-0180
The GiveHub JavaScript SDK provides a simple and intuitive way to interact with The Give Hub API. It includes support for campaign management, donations, impact tracking, and real-time notifications.
Latest release: https://thegivehub.com/sdk/givehub-sdk-js-latest.tgz
npm install givehub-sdk
Or using yarn:
yarn add givehub-sdk
Or by cloning the repo:
git clone https://github.com/thegivehub/sdk-js.git
Or by downloading the source:
wget https://thegivehub.com/sdk/givehub-sdk-js-latest.tgz
import { GiveHub } from 'givehub-sdk';
// Initialize the SDK
const givehub = new GiveHub({
baseUrl: 'https://api.thegivehub.com',
version: 'v1',
apiKey: 'your-api-key'
;
})
// Use the SDK
async function init() {
try {
const campaigns = await givehub.campaigns.list();
console.log('Active campaigns:', campaigns);
catch (error) {
} console.error('Error:', error);
} }
const result = await givehub.auth.login(email, password);
// SDK automatically handles token management
const userData = {
email: 'user@example.com',
password: 'secure123',
firstName: 'John',
lastName: 'Doe'
;
}await givehub.auth.register(userData);
await givehub.auth.verifyEmail(email, verificationCode);
const campaign = await givehub.campaigns.create({
title: 'Clean Water Project',
description: 'Providing clean water access',
targetAmount: 50000,
category: 'water',
milestones: [{
description: 'Phase 1',
amount: 10000
}]; })
const campaign = await givehub.campaigns.get(campaignId);
const campaigns = await givehub.campaigns.list({
category: 'water',
status: 'active',
page: 1,
limit: 10
; })
await givehub.campaigns.update(campaignId, {
title: 'Updated Title',
description: 'Updated description'
; })
await givehub.campaigns.uploadMedia(campaignId, file);
const donation = await givehub.donations.create({
campaignId: 'campaign-id',
amount: {
value: 100,
currency: 'USD'
,
}type: 'one-time'
; })
const recurring = await givehub.donations.createRecurring({
campaignId: 'campaign-id',
amount: {
value: 50,
currency: 'USD'
,
}frequency: 'monthly'
; })
const donations = await givehub.donations.getDonations({
campaignId: 'campaign-id',
status: 'completed'
; })
await givehub.donations.cancelRecurring(subscriptionId);
const metrics = await givehub.impact.createMetrics(campaignId, {
metrics: [{
name: 'People Helped',
value: 500,
unit: 'individuals'
}]; })
await givehub.impact.updateMetrics(metricId, {
value: 600,
verificationMethod: 'survey'
; })
const impact = await givehub.impact.getMetrics(campaignId, {
from: '2024-01-01',
to: '2024-12-31'
; })
const update = await givehub.updates.create({
campaignId: 'campaign-id',
title: 'Progress Update',
content: 'Project milestone achieved',
type: 'milestone'
; })
const updates = await givehub.updates.getUpdates({
campaignId: 'campaign-id',
type: 'milestone'
; })
await givehub.updates.uploadMedia(updateId, file);
const notifications = givehub.notifications;
// Connect
.connect();
notifications
// Listen for events
.on('donation_received', (notification) => {
notificationsconsole.log('New donation:', notification);
;
})
.on('milestone_completed', (notification) => {
notificationsconsole.log('Milestone completed:', notification);
;
})
// Disconnect when done
.disconnect(); notifications
The SDK throws GiveHubException
for API errors:
try {
await givehub.campaigns.create(campaignData);
catch (error) {
} if (error.status === 401) {
// Handle authentication error
else if (error.status === 400) {
} // Handle validation error
else {
} // Handle other errors
} }
interface Campaign {
: string;
id: string;
title: string;
description: number;
targetAmount: number;
currentAmount: string;
category: 'draft' | 'active' | 'completed' | 'suspended';
status: Milestone[];
milestones: string;
created: string;
updated }
interface Donation {
: string;
id: string;
campaignId: {
amount: number;
value: string;
currency;
}: 'one-time' | 'recurring';
type: 'pending' | 'completed' | 'failed';
status: {
transaction: string;
id: string;
status;
}: string;
created }
interface ImpactMetric {
: string;
name: number;
value: string;
unit?: string;
verificationMethod?: string;
verifiedAt?: string;
verifiedBy }
{baseUrl: string; // API base URL
version: string; // API version
apiKey: string; // Your API key
timeout: number; // Request timeout (ms)
retryAttempts: number; // Number of retry attempts
debug: boolean; // Enable debug logging
}
Token Management: The SDK automatically handles token refresh. Store tokens securely in your application.
Error Handling: Always wrap SDK calls in try-catch blocks for proper error handling.
Resource Cleanup: Disconnect from notifications when they’re no longer needed.
Rate Limiting: The SDK implements automatic rate limiting. Configure retry attempts based on your needs.
File Uploads: Use appropriate file types and sizes for media uploads.
async function manageCampaign() {
try {
// Create campaign
const campaign = await givehub.campaigns.create({
title: 'Water Project',
targetAmount: 50000
;
})
// Upload media
await givehub.campaigns.uploadMedia(
.id,
campaigndocument.querySelector('input[type="file"]').files[0]
;
)
// Track impact
await givehub.impact.createMetrics(campaign.id, {
metrics: [{
name: 'Wells Built',
value: 1,
unit: 'wells'
}];
})
// Post update
await givehub.updates.create({
campaignId: campaign.id,
title: 'First Well Complete',
content: 'We've completed the first well!'
;
})catch (error) {
} console.error('Campaign management failed:', error);
} }
async function processDonation(campaignId, amount) {
try {
// Create donation
const donation = await givehub.donations.create({
,
campaignIdamount: {
value: amount,
currency: 'USD'
};
})
// Track transaction
const transaction = await givehub.donations.getTransaction(
.transaction.id
donation;
)
// Show confirmation
if (transaction.status === 'completed') {
showSuccess('Donation successful!');
}catch (error) {
} console.error('Donation failed:', error);
} }
Enable debug mode to see detailed logs:
const givehub = new GiveHub({
debug: true,
// other config...
; })