const axios = require('axios');
class CardClanClient {
constructor(apiKey) {
this.apiKey = apiKey;
this.baseUrl = 'https://api.cardclan.com/api/integration';
this.headers = {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
};
}
async getWorkspaces() {
const response = await axios.get(`${this.baseUrl}/workspaces`, {
headers: this.headers
});
return response.data[0]?.choices || [];
}
async getCards(workspaceId) {
const response = await axios.post(`${this.baseUrl}/cards?workspace=${workspaceId}`,
{},
{ headers: this.headers }
);
return response.data[0]?.choices || [];
}
async createIntegrationConfig(userId, workspaceId, cardId) {
try {
const response = await axios.post(`${this.baseUrl}/config`, {
userId,
workspaceId,
cardId
}, { headers: this.headers });
return response.data.data._id;
} catch (error) {
if (error.response?.status === 400 &&
error.response.data.message.includes('already created')) {
// Config exists, get it
const existingConfig = await this.getIntegrationConfigByCard(cardId);
return existingConfig._id;
}
throw error;
}
}
async getIntegrationConfigByCard(cardId) {
const response = await axios.get(
`${this.baseUrl}/config/by-card?cardId=${cardId}`,
{ headers: this.headers }
);
return response.data.data;
}
async sendCard(cardId, integrationId, mergeTags, emailAccount = 'CardClan') {
const response = await axios.post(`${this.baseUrl}/send-card`, {
card: cardId,
emailAccount,
integrationId,
mergeTags: [mergeTags]
}, { headers: this.headers });
return response.data;
}
}
// Usage example
async function sendPersonalizedCard() {
const client = new CardClanClient(process.env.CARDCLAN_API_KEY);
try {
// 1. Get available workspaces
console.log('Fetching workspaces...');
const workspaces = await client.getWorkspaces();
const workspace = workspaces[0]; // Use first workspace
console.log(`Using workspace: ${workspace.name}`);
// 2. Get available cards
console.log('Fetching cards...');
const cards = await client.getCards(workspace.id);
const card = cards.find(c => c.title.includes('Welcome')) || cards[0];
console.log(`Using card: ${card.title}`);
// 3. Create or get integration configuration
console.log('Setting up integration configuration...');
const integrationId = await client.createIntegrationConfig(
'your-user-id',
workspace.id,
card.id
);
// 4. Send the card
console.log('Sending card...');
const result = await client.sendCard(card.id, integrationId, {
name: 'Alex Thompson',
email: 'alex@example.com',
company: 'Innovation Labs',
position: 'Product Manager',
joinDate: new Date().toLocaleDateString()
});
console.log('✅ Success!');
console.log(`Card sent to: ${result.message}`);
console.log(`Tracking URL: ${result.tracking_url}`);
return result;
} catch (error) {
console.error('❌ Error sending card:', error.message);
if (error.response) {
console.error('API Response:', error.response.data);
}
throw error;
}
}
// Run the example
sendPersonalizedCard()
.then(() => console.log('Done!'))
.catch(error => console.error('Failed:', error.message));