Server Setup
Build a complete backend server that your frontend applications can connect to. Choose your preferred framework below.
Project Setup
mkdir my-dcid-server
cd my-dcid-server
npm init -y
npm install @dcid/server-sdk express cors dotenv
npm install -D typescript @types/node @types/express ts-node nodemonTypeScript Configuration
Package Scripts
Python Project Structure
Requirements File
Go Project Structure
Environment Configuration
Create a `.env` file in your project root:
Complete Server Implementation
// src/index.ts
import 'dotenv/config';
import express, { Request, Response, NextFunction } from 'express';
import cors from 'cors';
import { DCIDServerSDK, DCIDServerSDKError } from '@dcid/server-sdk';
// Initialize SDK
const sdk = new DCIDServerSDK({
apiKey: process.env.DCID_API_KEY!,
environment: (process.env.DCID_ENVIRONMENT as 'dev' | 'prod') || 'prod',
enableRequestLogging: process.env.NODE_ENV !== 'production'
});
const app = express();
// Middleware
app.use(cors());
app.use(express.json());
// Request logging
app.use((req, res, next) => {
console.log(`${req.method} ${req.path}`);
next();
});
// ============================================
// Authentication Endpoints
// ============================================
// Initiate OTP
app.post('/auth/initiate', async (req, res, next) => {
try {
const { email, phone } = req.body;
const result = await sdk.auth.registerOTP({ email, phone });
res.json(result);
} catch (error) {
next(error);
}
});
// Confirm OTP
app.post('/auth/confirm', async (req, res, next) => {
try {
const { email, phone, code } = req.body;
const result = await sdk.auth.confirmOTP({
email,
phone,
otp: code
});
res.json(result);
} catch (error) {
next(error);
}
});
// Refresh token
app.post('/auth/refresh', async (req, res, next) => {
try {
const { refreshToken } = req.body;
const result = await sdk.auth.refreshToken({ refreshToken });
res.json(result);
} catch (error) {
next(error);
}
});
// ============================================
// Identity - Encryption Endpoints
// ============================================
app.post('/identity/encryption/generate', async (req, res, next) => {
try {
const { did, ownerEmail } = req.body;
const result = await sdk.identity.encryption.generateKey({
did,
ownerEmail
});
res.json(result);
} catch (error) {
next(error);
}
});
app.post('/identity/encryption/get', async (req, res, next) => {
try {
const { did } = req.body;
const result = await sdk.identity.encryption.getKey({ did });
res.json(result);
} catch (error) {
next(error);
}
});
// ============================================
// Identity - Issuer Endpoints
// ============================================
app.post('/identity/issuer/issue', async (req, res, next) => {
try {
const result = await sdk.identity.issuer.issueCredential(req.body);
res.json(result);
} catch (error) {
next(error);
}
});
app.get('/identity/issuer/offer/:claimId', async (req, res, next) => {
try {
const result = await sdk.identity.issuer.getCredentialOffer({
claimId: req.params.claimId
});
res.json(result);
} catch (error) {
next(error);
}
});
// ============================================
// Identity - IPFS Endpoints
// ============================================
app.post('/identity/ipfs/store', async (req, res, next) => {
try {
const result = await sdk.identity.ipfs.storeCredential(req.body);
res.json(result);
} catch (error) {
next(error);
}
});
app.post('/identity/ipfs/retrieve', async (req, res, next) => {
try {
const result = await sdk.identity.ipfs.retrieveUserCredential(req.body);
res.json(result);
} catch (error) {
next(error);
}
});
app.post('/identity/ipfs/all', async (req, res, next) => {
try {
const result = await sdk.identity.ipfs.getAllUserCredentials(req.body);
res.json(result);
} catch (error) {
next(error);
}
});
// ============================================
// Identity - Verification Endpoints
// ============================================
app.post('/identity/verify/initiate', async (req, res, next) => {
try {
const result = await sdk.identity.verification.verifySignIn(req.body);
res.json(result);
} catch (error) {
next(error);
}
});
app.get('/identity/verify/link-store/:sessionId', async (req, res, next) => {
try {
const result = await sdk.identity.verification.getLinkStore({
id: req.params.sessionId
});
res.json(result);
} catch (error) {
next(error);
}
});
app.post('/identity/verify/callback', async (req, res, next) => {
try {
const { sessionId } = req.query;
const result = await sdk.identity.verification.verifyCallback({
sessionId: sessionId as string,
jwzToken: req.body.jwzToken
});
res.json(result);
} catch (error) {
next(error);
}
});
// ============================================
// Error Handler
// ============================================
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
console.error('Error:', err);
if (err instanceof DCIDServerSDKError) {
return res.status(err.statusCode || 500).json({
error: err.message,
code: err.statusCode
});
}
res.status(500).json({
error: 'Internal server error'
});
});
// ============================================
// Start Server
// ============================================
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log(`Environment: ${process.env.DCID_ENVIRONMENT}`);
});PHP: add the following routes in `routes/api.php`:
Running the Server
# Development
npm run dev
# Production
npm run build
npm startTesting with curl
Connecting Frontend SDK
Your frontend applications connect to this server:
Production Deployment
Node.js Docker
Build and Run
Python Docker
Build and Run
Go Docker
Build and Run
PHP Deployment
Environment Variables
Set these in your deployment platform:
| Variable | Required | Description |
|---|---|---|
| DCID_API_KEY | Yes | Your DCID API key |
| DCID_ENVIRONMENT | No | prod (default) or dev |
| PORT | No | Server port (default: 3001) |
Next Steps
- API Reference — Complete SDK documentation
- Frontend Integration — Connect your frontend application