SDK/Library Implementation for SaaS Application Integrations
SDK (Software Development Kit) is a ready-made library for API clients. It lowers the barrier to integration: instead of working with HTTP directly, developers use typed methods.
SDK project structure
my-api-sdk/
src/
client.ts # Main HTTP client
resources/
users.ts # Resource: users
projects.ts # Resource: projects
webhooks.ts # Webhook verification
types/
index.ts # All public types
responses.ts # API response types
errors.ts # Custom errors
retry.ts # Retry logic
pagination.ts # Pager for lists
tests/
client.test.ts
dist/ # Compiled JS + types
package.json
tsconfig.json
README.md
HTTP client
// src/client.ts
export interface MyApiConfig {
apiKey: string;
baseUrl?: string;
timeout?: number;
maxRetries?: number;
}
export class MyApiError extends Error {
constructor(
message: string,
public readonly statusCode: number,
public readonly code: string,
public readonly requestId: string
) {
super(message);
this.name = 'MyApiError';
}
}
export class MyApiClient {
private readonly baseUrl: string;
private readonly apiKey: string;
private readonly timeout: number;
private readonly maxRetries: number;
// Resources
public readonly users: UsersResource;
public readonly projects: ProjectsResource;
public readonly webhooks: WebhooksResource;
constructor(config: MyApiConfig) {
this.baseUrl = config.baseUrl ?? 'https://api.myproduct.com/v1';
this.apiKey = config.apiKey;
this.timeout = config.timeout ?? 30_000;
this.maxRetries = config.maxRetries ?? 3;
this.users = new UsersResource(this);
this.projects = new ProjectsResource(this);
this.webhooks = new WebhooksResource(this);
}
async request<T>(
method: string,
path: string,
options: RequestOptions = {}
): Promise<T> {
const url = `${this.baseUrl}${path}`;
const headers: Record<string, string> = {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
'User-Agent': `myapi-sdk-node/${SDK_VERSION}`,
'X-SDK-Version': SDK_VERSION,
};
let lastError: Error | undefined;
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
if (attempt > 0) {
// Exponential backoff: 1s, 2s, 4s
await new Promise(resolve => setTimeout(resolve, 2 ** (attempt - 1) * 1000));
}
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
const response = await fetch(url, {
method,
headers,
body: options.body ? JSON.stringify(options.body) : undefined,
signal: controller.signal,
});
clearTimeout(timeoutId);
const requestId = response.headers.get('x-request-id') ?? 'unknown';
if (!response.ok) {
const error = await response.json().catch(() => ({}));
// Don't retry client errors
if (response.status < 500) {
throw new MyApiError(
error.message ?? 'API Error',
response.status,
error.code ?? 'UNKNOWN',
requestId
);
}
lastError = new MyApiError(
error.message ?? 'Server Error',
response.status,
error.code ?? 'SERVER_ERROR',
requestId
);
continue;
}
if (response.status === 204) return undefined as T;
return response.json();
} catch (error) {
if (error instanceof MyApiError) throw error;
lastError = error as Error;
}
}
throw lastError;
}
}
Resource with pagination
// src/resources/projects.ts
export interface Project {
id: string;
name: string;
status: 'active' | 'archived';
createdAt: string;
}
export interface ListProjectsParams {
limit?: number;
cursor?: string;
status?: 'active' | 'archived';
}
export interface PaginatedResponse<T> {
data: T[];
nextCursor?: string;
hasMore: boolean;
total: number;
}
export class ProjectsResource {
constructor(private client: MyApiClient) {}
async list(params: ListProjectsParams = {}): Promise<PaginatedResponse<Project>> {
const query = new URLSearchParams();
if (params.limit) query.set('limit', params.limit.toString());
if (params.cursor) query.set('cursor', params.cursor);
if (params.status) query.set('status', params.status);
return this.client.request('GET', `/projects?${query}`);
}
// Auto-paging iterator
async *listAll(params: Omit<ListProjectsParams, 'cursor'> = {}): AsyncIterable<Project> {
let cursor: string | undefined;
do {
const page = await this.list({ ...params, cursor, limit: params.limit ?? 100 });
yield* page.data;
cursor = page.nextCursor;
} while (cursor);
}
async create(data: { name: string; description?: string }): Promise<Project> {
return this.client.request('POST', '/projects', { body: data });
}
async get(id: string): Promise<Project> {
return this.client.request('GET', `/projects/${id}`);
}
async update(id: string, data: Partial<Pick<Project, 'name' | 'status'>>): Promise<Project> {
return this.client.request('PATCH', `/projects/${id}`, { body: data });
}
async delete(id: string): Promise<void> {
return this.client.request('DELETE', `/projects/${id}`);
}
}
Webhook verification
// src/resources/webhooks.ts
import { createHmac, timingSafeEqual } from 'crypto';
export class WebhooksResource {
constructor(private client: MyApiClient) {}
verify(payload: string | Buffer, signature: string, secret: string): boolean {
const expectedSignature = 'sha256=' + createHmac('sha256', secret)
.update(payload)
.digest('hex');
const sigBuffer = Buffer.from(signature);
const expectedBuffer = Buffer.from(expectedSignature);
if (sigBuffer.length !== expectedBuffer.length) return false;
return timingSafeEqual(sigBuffer, expectedBuffer);
}
constructEvent(
payload: string,
signature: string,
secret: string
): WebhookEvent {
if (!this.verify(payload, signature, secret)) {
throw new Error('Invalid webhook signature');
}
return JSON.parse(payload);
}
}
Publishing to npm
{
"name": "@mycompany/api-sdk",
"version": "1.0.0",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"files": ["dist"],
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts",
"test": "vitest run",
"prepublishOnly": "npm run build && npm test"
}
}
// Using SDK
import { MyApiClient } from '@mycompany/api-sdk';
const api = new MyApiClient({ apiKey: process.env.MYAPI_KEY! });
// Simple operations
const project = await api.projects.create({ name: 'New Project' });
// Auto-paging
for await (const project of api.projects.listAll({ status: 'active' })) {
console.log(project.name);
}
// Webhook verification
const event = api.webhooks.constructEvent(rawBody, signature, webhookSecret);
Developing TypeScript SDK with auto-retries, pagination and npm publishing — 3–5 working days.







