Guides
JavaScript & Node.js
Call the API from your server with fetch; your browser code talks only to your server.
Node.js (server)
// Node.js 18+ (server-side only — never ship your API key to the browser)
const res = await fetch("https://oms-api.mainconsulting.co.uk/api/public/v1/search", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OMS_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ query: "I need first aid training for my staff.", context: null }),
signal: AbortSignal.timeout(30_000),
});
const data = await res.json();
if (!res.ok) {
console.error(res.status, data.error.code, data.request_id);
} else {
console.log(data.answer);
for (const c of data.courses) console.log("-", c.name, c.events.length, "dates");
// Store data.context for the visitor's next question.
}Browser (front end)
Never call the OMS API directly from the browser. Your key would be visible to every visitor, and the API sends no CORS headers, so the browser blocks the request anyway.
// Browser code calls YOUR backend (e.g. the WordPress route above), never the OMS API directly.
async function searchCourses(query, { newSearch = false } = {}) {
const res = await fetch("/wp-json/oms/v1/search", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query, visitor: localStorage.getItem("omsVisitor"), new_search: newSearch }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error ?? "Search failed");
localStorage.setItem("omsVisitor", data.visitor);
return data; // same shape as the API response, minus "context"
}cURL
curl https://oms-api.mainconsulting.co.uk/api/public/v1/search \
-H "Authorization: Bearer $OMS_API_KEY" \
-H "Content-Type: application/json" \
-d '{"query": "I need first aid training for my staff."}'