JavaScript SDKJavaScript SDK Getting Started
JavaScript SDK Getting Started
Install and configure the SocialRails JavaScript/TypeScript SDK.
The official JavaScript/TypeScript SDK for the SocialRails API. Zero dependencies, full TypeScript support, works in Node.js 18+, Deno, Bun, and modern browsers.
Installation
npm install @socialrails/jsyarn add @socialrails/jspnpm add @socialrails/jsQuick Start
const sr = new SocialRails({ apiKey: 'sr_live_...' });
const post = await sr.posts.create({
content: 'Hello from SocialRails!',
platform: 'twitter',
scheduled_for: '2026-03-10T14:00:00Z',
});
console.log(post.id, post.status);Configuration
You can also set the SOCIALRAILS_API_KEY environment variable:
export SOCIALRAILS_API_KEY=sr_live_your_key_hereconst sr = new SocialRails(); // picks up the env varError Handling
All API errors are thrown as SocialRailsError instances:
try {
await sr.posts.create({ content: '', platform: 'twitter' });
} catch (error) {
if (error instanceof SocialRailsError) {
console.error(error.message); // Human-readable message
console.error(error.code); // e.g. "validation_error"
console.error(error.status); // e.g. 422
}
}TypeScript
The SDK ships with complete type definitions:
Requirements
- Node.js 18+ (uses native
fetch) - Deno — works out of the box
- Bun — works out of the box
- Browsers — any modern browser with
fetchsupport
Next Steps
See the JavaScript SDK Reference for the full API reference.