Skip to main content

Node.js Examples

Node.js examples using the native fetch API (Node.js 18+).

Authentication

const BASE_URL = 'https://staging.api.ecourtdate.com';

async function getToken(clientId, clientSecret) {
const response = await fetch(`${BASE_URL}/oauth/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
client_id: clientId,
client_secret: clientSecret,
grant_type: 'client_credentials',
}),
});
const data = await response.json();
return data.access_token;
}

Send a Message

async function sendMessage(token, to, subject, content) {
const response = await fetch(`${BASE_URL}/v1/messages/oneoffs`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ to, subject, content }),
});
return response.json();
}

List Clients

async function listClients(token, limit = 25) {
const response = await fetch(`${BASE_URL}/v1/clients?limit=${limit}`, {
headers: { 'Authorization': `Bearer ${token}` },
});
return response.json();
}

Create Client

async function createClient(token, clientData) {
const response = await fetch(`${BASE_URL}/v1/clients`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(clientData),
});
return response.json();
}

Webhook Listener

See Webhook Examples for a complete implementation.