Unipile Node.js SDK
The Unipile Node.js SDK (version 1.9.3) provides a unified interface for integrating with multiple communication platforms, including LinkedIn, WhatsApp, Instagram, Telegram, and a generic Email API. It allows developers to programmatically manage social connections, send and receive messages (including LinkedIn InMail), retrieve user and company profiles, handle invitations, manage posts, and control email communications. This SDK is particularly useful for applications requiring multi-channel outreach, unified inboxes, or automation across various professional and messaging platforms, abstracting the complexities and individual APIs of each service. The SDK is actively maintained with regular updates reflecting new features and API changes across its supported providers.
Common errors
-
Error: connect ECONNREFUSED
cause The Unipile API endpoint (DSN) is unreachable, possibly due to a network issue, incorrect DSN, or Unipile service outage.fixVerify your `UNIPIL_DSN` configuration. Check network connectivity and Unipile's service status page. Ensure your firewall isn't blocking outgoing connections. -
Error: Unauthorized
cause The provided `UNIPIL_ACCESS_TOKEN` is missing, expired, or invalid, or the connected account is disconnected from the provider.fixEnsure your `UNIPIL_ACCESS_TOKEN` is correct and current. If an account is disconnected, prompt the user to re-authenticate via the hosted authentication flow. -
Error: Too Many Requests
cause Your application has exceeded the API rate limits imposed by Unipile or the underlying social media platform (e.g., LinkedIn).fixImplement exponential back-off and retry logic for API calls. Review and adjust your application's usage patterns to stay within documented rate limits. -
TypeError: client.users.getCompanyProfile is not a function
cause This typically occurs when `unipile-node-sdk` is imported using CommonJS `require()` syntax in an environment expecting ESM, or if the client object is not correctly initialized.fixEnsure you are using `import { UnipileClient } from 'unipile-node-sdk';` and that your `package.json` specifies `"type": "module"` or your file uses `.mjs` extension for ESM. Verify the client is initialized before calling methods. -
Error: Invalid account
cause The `account_id` provided for an operation is incorrect, not found, or not associated with the requested feature or provider.fixVerify that the `account_id` used in the API call is valid and corresponds to an actively connected account for the specific provider (e.g., LinkedIn for `getCompanyProfile`). Double-check stored `account_id` values.
Warnings
- breaking The Unipile Node.js SDK requires Node.js version 18 or higher. Applications running on older Node.js versions will encounter compatibility issues or fail to run.
- gotcha API Access Tokens and DSN (Data Source Name) are sensitive credentials. Exposing them in client-side code or committing them directly to version control poses a significant security risk.
- gotcha Interacting with social media APIs (LinkedIn, WhatsApp, Instagram) is subject to strict rate limits and terms of service. Excessive or automated activity can lead to temporary blocks or permanent account suspension.
- gotcha Authentication processes for platforms like LinkedIn can be complex, involving hosted auth flows or direct username/password methods which may require handling 2FA/OTP checkpoints. Incorrect implementation can lead to connection failures or account lockouts.
- gotcha The underlying APIs (LinkedIn, WhatsApp, etc.) frequently update, which may introduce breaking changes not immediately reflected in the SDK. This can lead to unexpected errors or outdated functionality.
Install
-
npm install unipile-node-sdk -
yarn add unipile-node-sdk -
pnpm add unipile-node-sdk
Imports
- UnipileClient
const UnipileClient = require('unipile-node-sdk')import { UnipileClient } from 'unipile-node-sdk' - UnipileError
import { UnipileError } from 'unipile-node-sdk' - AccountConnectLinkedinInput
import type { AccountConnectLinkedinInput } from 'unipile-node-sdk'
Quickstart
import { UnipileClient } from 'unipile-node-sdk';
// Initialize the Unipile client with your DSN and Access Token.
// Always use environment variables for sensitive credentials in production.
const client = new UnipileClient(
process.env.UNIPIL_DSN ?? 'https://api.unipile.com', // Default or example DSN
process.env.UNIPIL_ACCESS_TOKEN ?? '' // Your Unipile API Access Token
);
async function runUnipileExample() {
try {
console.log('Attempting to generate a Hosted Auth Link...');
// Generates a link for users to connect their social accounts securely.
const authLinkResponse = await client.account.createHostedAuthLink({
type: 'create', // 'create' for new accounts, 'reconnect' for existing
expiresOn: new Date(Date.now() + 3600 * 1000).toISOString(), // Link valid for 1 hour
api_url: process.env.UNIPIL_API_URL ?? 'https://api.unipile.com', // Your Unipile API URL
providers: '*', // '*' for all providers, or an array like ['LINKEDIN', 'WHATSAPP']
success_redirect_url: 'https://your-app.com/auth-success', // Where to redirect after successful connection
metadata: { userId: 'user-123', integrationType: 'onboarding' } // Optional metadata
});
console.log('Hosted Auth Link Generated:', authLinkResponse.url);
// Example: Retrieve a LinkedIn company profile (requires a connected LinkedIn account_id).
// This account_id would typically be stored after a user successfully connects via the auth link.
const linkedinAccountId = process.env.LINKEDIN_ACCOUNT_ID; // Example: 'acc_xyz123'
if (linkedinAccountId) {
console.log(`\nRetrieving LinkedIn company profile for account ${linkedinAccountId}...`);
const companyProfile = await client.users.getCompanyProfile({
account_id: linkedinAccountId,
identifier: 'Unipile', // The company's name or LinkedIn ID
});
console.log('Retrieved Company Profile:', companyProfile);
} else {
console.warn('\nWarning: LINKEDIN_ACCOUNT_ID environment variable not set. Skipping company profile example.');
}
} catch (error) {
console.error('\nAn error occurred during Unipile SDK operation:');
if (error instanceof Error) {
console.error('Error message:', error.message);
// Log the full error object for debugging in development
if (process.env.NODE_ENV !== 'production') {
console.error(error);
}
} else {
console.error(error);
}
}
}
runUnipileExample();