Skip to main content

Installation

npm install @dataferry/sdk

Initialize

import { Ferry } from '@dataferry/sdk';

const ferry = new Ferry({
  apiKey: process.env.DATAFERRY_API_KEY,
  baseUrl: 'https://api.dataferry.dev', // optional, defaults to this
  timeout: 30000, // optional, request timeout in ms
});

ferry.createPortalSession()

Create a portal session for a user to export their data.
const session = await ferry.createPortalSession({
  scopeId: 'user_123',           // Required for multi-tenant
  connectionId: 'conn_abc',      // Optional, uses primary if omitted
  returnUrl: 'https://...',      // Where to redirect after
  expiresIn: 3600,               // Session duration in seconds (default: 3600)
  metadata: { plan: 'pro' },     // Custom data attached to session
});
Returns:
{
  id: 'ps_abc123',
    url: 'https://portal.dataferry.dev/s/abc123',
    expiresAt: Date
}

Portal Sessions

Advanced portal session management.

ferry.portalSessions.create()

Same as createPortalSession(). Provided for consistency with resource-style APIs.
const session = await ferry.portalSessions.create({
  scopeId: 'user_123',
  returnUrl: 'https://...',
});

ferry.portalSessions.retrieve()

Get session details.
const session = await ferry.portalSessions.retrieve('ps_abc123');
// { id, scopeId, expiresAt, isExpired, ... }

ferry.portalSessions.revoke()

Invalidate a session early.
await ferry.portalSessions.revoke('ps_abc123');

Exports

Manage export jobs programmatically.

ferry.exports.create()

Start an export without the portal UI.
const exportJob = await ferry.exports.create({
  scopeId: 'user_123',
  connectionId: 'conn_abc',  // optional
  format: 'csv',
  tables: ['conversations', 'messages'],  // optional, defaults to all
});

ferry.exports.retrieve()

Get export status and download URL.
const exportJob = await ferry.exports.retrieve('exp_abc123');

if (exportJob.status === 'completed') {
  console.log(exportJob.downloadUrl);
}

ferry.exports.list()

List exports with optional filters.
const { data, meta } = await ferry.exports.list({
  scopeId: 'user_123',
  connectionId: 'conn_abc',
  status: 'completed',
  page: 1,
  limit: 20,
});

ferry.exports.poll()

Wait for an export to complete.
const completed = await ferry.exports.poll('exp_abc123', {
  maxWait: 300000,  // 5 minutes
  interval: 2000,   // check every 2 seconds
});

Connections

Manage database connections.

ferry.connections.list()

const connections = await ferry.connections.list();

ferry.connections.create()

const connection = await ferry.connections.create({
  name: 'Production DB',
  host: 'db.example.com',
  port: '5432',
  database: 'myapp',
  user: 'ferry_readonly',
  password: 'secret',
  ssl: true,
});

ferry.connections.retrieve()

const connection = await ferry.connections.retrieve('conn_abc123');

ferry.connections.update()

const connection = await ferry.connections.update('conn_abc123', {
  name: 'Production DB (Updated)',
});

ferry.connections.delete()

await ferry.connections.delete('conn_abc123');

ferry.connections.test()

const result = await ferry.connections.test('conn_abc123');
if (result.connected) {
  console.log('Connected successfully');
}

ferry.connections.setPrimary()

await ferry.connections.setPrimary('conn_abc123');

Errors

import { Ferry, FerryError } from '@dataferry/sdk';

try {
  await ferry.createPortalSession({ scopeId: 'user_123' });
} catch (error) {
  if (error instanceof FerryError) {
    console.log(error.code);    // 'INVALID_REQUEST'
    console.log(error.message); // 'Invalid scopeId'
    console.log(error.status);  // 400
  }
}

TypeScript

All methods are fully typed. Import types as needed:
import type {
  FerryConfig,
  CreatePortalSessionParams,
  PortalSessionResult,
  ExportJob,
  DatabaseConnection,
} from '@dataferry/sdk';