Chaindoc SDKs
Official SDKs for seamless Chaindoc integration. Full TypeScript support, zero dependencies, and framework-agnostic design for both backend and frontend applications.
Available SDKs
- Server SDK (@chaindoc_io/server-sdk) - Backend integration for Node.js 18+
- Embed SDK (@chaindoc_io/embed-sdk) - Frontend signing interface for web applications
- Python SDK - Coming soon
- PHP SDK - Coming soon
Server SDK
The Server SDK provides a type-safe Node.js interface for the Chaindoc API. Use it to manage documents, create signature requests, handle file uploads, and integrate blockchain verification into your backend.
Installation
npm install @chaindoc_io/server-sdkQuick Start
import { Chaindoc } from '@chaindoc_io/server-sdk';
import { readFile } from 'fs/promises';
// 1. Initialize the SDK
const chaindoc = new Chaindoc({
secretKey: process.env.CHAINDOC_SECRET_KEY!,
});
// 2. Upload a document file
const buffer = await readFile('./contract.pdf');
const file = new Blob([buffer], { type: 'application/pdf' });
const { media } = await chaindoc.media.upload([file]);
// 3. Create a document
const doc = await chaindoc.documents.create({
name: 'Service Agreement',
description: 'Contract for consulting services',
media: media[0],
status: 'published', // Triggers blockchain verification
hashtags: ['#contract', '#2024'],
meta: [{ key: 'client', value: 'Acme Corp' }],
});
// 4. Create a signature request
const sigRequest = await chaindoc.signatures.createRequest({
versionId: doc.document.versions[0].uuid,
recipients: [{ email: 'signer@example.com' }],
deadline: new Date('2024-12-31'),
embeddedFlow: true,
});
// 5. Create session for frontend SDK
const session = await chaindoc.embedded.createSession({
email: 'signer@example.com',
metadata: {
documentId: doc.documentId,
signatureRequestId: sigRequest.signatureRequest.uuid,
},
});
console.log('Session ID:', session.sessionId);Express.js Integration
import express from 'express';
import { Chaindoc, ChaindocError } from '@chaindoc_io/server-sdk';
import multer from 'multer';
const app = express();
const upload = multer({ storage: multer.memoryStorage() });
const chaindoc = new Chaindoc({
secretKey: process.env.CHAINDOC_SECRET_KEY!,
});
// Upload and create document
app.post('/api/documents', upload.single('file'), async (req, res) => {
try {
const file = new Blob([req.file!.buffer], { type: req.file!.mimetype });
const { media } = await chaindoc.media.upload([file]);
const doc = await chaindoc.documents.create({
name: req.body.name,
description: req.body.description || '',
media: media[0],
status: 'published',
hashtags: req.body.hashtags || [],
meta: req.body.meta || [],
});
res.json({ documentId: doc.documentId });
} catch (error) {
if (error instanceof ChaindocError) {
res.status(error.statusCode || 500).json({ error: error.message });
} else {
res.status(500).json({ error: 'Internal server error' });
}
}
});
// Create embedded session for signer
app.post('/api/signing/session', async (req, res) => {
try {
const { email, documentId, signatureRequestId } = req.body;
const session = await chaindoc.embedded.createSession({
email,
metadata: { documentId, signatureRequestId },
});
res.json({ sessionId: session.sessionId });
} catch (error) {
if (error instanceof ChaindocError) {
res.status(error.statusCode || 500).json({ error: error.message });
}
}
});
app.listen(3000);Next.js API Routes
import { NextRequest, NextResponse } from 'next/server';
import { Chaindoc, ChaindocError } from '@chaindoc_io/server-sdk';
const chaindoc = new Chaindoc({
secretKey: process.env.CHAINDOC_SECRET_KEY!,
});
export async function POST(request: NextRequest) {
try {
const { email, documentId, signatureRequestId } = await request.json();
const session = await chaindoc.embedded.createSession({
email,
metadata: { documentId, signatureRequestId },
});
return NextResponse.json({
sessionId: session.sessionId,
expiresAt: session.expiresAt,
});
} catch (error) {
if (error instanceof ChaindocError) {
return NextResponse.json(
{ error: error.message },
{ status: error.statusCode || 500 }
);
}
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}Error Handling
Always wrap API calls in try-catch blocks and handle ChaindocError specifically:
import { ChaindocError } from '@chaindoc_io/server-sdk';
try {
const doc = await chaindoc.documents.create({ /* ... */ });
} catch (error) {
if (error instanceof ChaindocError) {
console.error('API Error:', error.message);
console.error('Status Code:', error.statusCode);
switch (error.statusCode) {
case 400:
// Bad request - check parameters
break;
case 401:
// Unauthorized - check API key
break;
case 404:
// Not found
break;
case 429:
// Rate limited - SDK will auto-retry
break;
}
}
}Embed SDK
The Embed SDK enables seamless integration of document signing functionality into web applications. It manages an iframe-based signing flow with secure communication between your application and Chaindoc.
Installation
npm install @chaindoc_io/embed-sdkBasic Usage
import { ChaindocEmbed } from '@chaindoc_io/embed-sdk';
// 1. Initialize SDK (once per page)
const chaindoc = new ChaindocEmbed({
publicKey: 'pk_live_xxxxxxxxxxxxx',
environment: 'production',
});
// 2. Get session from your backend
const response = await fetch('/api/signing/create-session', {
method: 'POST',
body: JSON.stringify({ documentId, signerEmail }),
});
const { sessionId } = await response.json();
// 3. Open signing flow
const instance = chaindoc.openSignatureFlow({
sessionId,
onReady: () => {
console.log('Signing interface loaded');
},
onSuccess: (data) => {
console.log('Document signed:', data.signatureId);
instance.close();
},
onError: (error) => {
console.error('Signing failed:', error.code, error.message);
},
onCancel: () => {
console.log('User cancelled');
instance.close();
},
});React Integration
import { useCallback, useRef, useEffect } from 'react';
import { ChaindocEmbed, EmbedInstance } from '@chaindoc_io/embed-sdk';
function SignButton({ sessionId }: { sessionId: string }) {
const sdkRef = useRef<ChaindocEmbed | null>(null);
const instanceRef = useRef<EmbedInstance | null>(null);
useEffect(() => {
sdkRef.current = new ChaindocEmbed({
publicKey: process.env.REACT_APP_CHAINDOC_PUBLIC_KEY!,
});
return () => {
sdkRef.current?.destroy();
};
}, []);
const handleSign = useCallback(() => {
if (!sdkRef.current) return;
instanceRef.current = sdkRef.current.openSignatureFlow({
sessionId,
onSuccess: (data) => {
console.log('Signed!', data.signatureId);
instanceRef.current?.close();
},
onCancel: () => {
instanceRef.current?.close();
},
});
}, [sessionId]);
return <button onClick={handleSign}>Sign Document</button>;
}Vue 3 Integration
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { ChaindocEmbed, type EmbedInstance } from '@chaindoc_io/embed-sdk';
const props = defineProps<{ sessionId: string }>();
let sdk: ChaindocEmbed | null = null;
let instance: EmbedInstance | null = null;
onMounted(() => {
sdk = new ChaindocEmbed({
publicKey: import.meta.env.VITE_CHAINDOC_PUBLIC_KEY,
});
});
onUnmounted(() => {
sdk?.destroy();
});
function openSignature() {
if (!sdk) return;
instance = sdk.openSignatureFlow({
sessionId: props.sessionId,
onSuccess: (data) => {
console.log('Signed!', data.signatureId);
instance?.close();
},
onCancel: () => {
instance?.close();
},
});
}
</script>
<template>
<button @click="openSignature">Sign Document</button>
</template>Inline Mode
Instead of a modal, embed the signing interface directly into your page:
const instance = chaindoc.openSignatureFlow({
sessionId,
mode: 'inline',
container: document.getElementById('signature-container'),
onSuccess: (data) => {
console.log('Signed!');
},
});Theming
Customize the appearance with light or dark themes:
const instance = chaindoc.openSignatureFlow({
sessionId,
theme: 'dark',
// ... other options
});
// Change theme dynamically
instance.changeTheme('light');Complete Workflow Example
Here's a complete end-to-end example combining Server SDK and Embed SDK:
1Backend: Upload DocumentUse Server SDK to upload file and create document
2Backend: Create Signature RequestCreate signature request with embedded flow enabled
3Backend: Generate SessionCreate embedded session for each signer
4Frontend: Initialize Embed SDKInitialize SDK with public key
5Frontend: Open Signing FlowOpen signing interface with session ID
6Frontend: Handle SuccessProcess signed document and update UI
// server.ts
import { Chaindoc } from '@chaindoc_io/server-sdk';
const chaindoc = new Chaindoc({
secretKey: process.env.CHAINDOC_SECRET_KEY!,
});
// Upload & create document
const { media } = await chaindoc.media.upload([pdfFile]);
const doc = await chaindoc.documents.create({
name: 'Contract',
description: 'Service agreement',
media: media[0],
status: 'published',
hashtags: ['#contract'],
meta: [],
});
// Create signature request
const sigRequest = await chaindoc.signatures.createRequest({
versionId: doc.document.versions[0].uuid,
recipients: [{ email: 'signer@example.com' }],
deadline: new Date('2024-12-31'),
embeddedFlow: true,
});
// Create session
const session = await chaindoc.embedded.createSession({
email: 'signer@example.com',
metadata: {
documentId: doc.documentId,
signatureRequestId: sigRequest.signatureRequest.uuid,
},
});
// Return sessionId to frontend
res.json({ sessionId: session.sessionId });Best Practices
- Initialize SDK once per page/component lifecycle
- Always destroy SDK instance on component unmount
- Handle all callback events (onSuccess, onError, onCancel)
- Store API keys in environment variables
- Use TypeScript for better type safety
- Implement proper error handling with ChaindocError
- Test with sandbox keys before production deployment
Environment Configuration
// Backend
const chaindoc = new Chaindoc({
secretKey: 'sk_live_xxxxx',
});
// Frontend
const embed = new ChaindocEmbed({
publicKey: 'pk_live_xxxxx',
environment: 'production',
});