Back end
ComPilot recommends that you create your own API using @compilot/js-sdk
. There are two things you have to do.
Create an API using the
CreateApiClient
object.Create an endpoint that calls the
apiClient.createSession()
method.
Step by step integration
Install
@compilot/js-sdk
.- yarn
- npm
- pnpm
yarn add @compilot/js-sdk
npm install @compilot/js-sdk
pnpm add @compilot/js-sdk
Import and instantiate
createApiClient
using your ComPilot Api Key.import { createSdk } from "@compilot/js-sdk";
const apiClient = createSdk({
// webhookSecret is optional
webhookSecret: process.env.WEBHOOK_SECRET,
apiKey: process.env.API_KEY,
});WEBHOOK_SECRET
is the value of the webhook secret parameter.Create an endpoint returning the result of
createSession
. You must pass a customer ID in this API call.- Express
- Hono
- Next.js
// app is your express app
app.post("/api/create-compilot-session", async (req, res) => {
const sessionRes = await apiClient.createSession({
workflowId: process.env.REGULAR_WORKFLOW_ID,
externalCustomerId: req.session.userId,
});
res.status(200).json(sessionRes);
});REGULAR_WORKFLOW_ID
is the workflow ID.app.post("/my-api/create-compilot-session", async (c) => {
const sessionRes = await apiClient.createSession({
workflowId: WORKFLOW_ID,
externalCustomerId: req.session.userId,
});
return c.json(sessionRes);
});To find the value of
WORKFLOW_ID
see the Workflow detail page.import type { NextApiRequest, NextApiResponse } from "next";
import { createSdk } from "@compilot/js-sdk";
const apiClient = createSdk({
// webhookSecret is optional
webhookSecret: process.env.WEBHOOK_SECRET!,
apiKey: process.env.API_KEY!,
});
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") {
res.setHeader("Allow", ["POST"]);
res.status(405).end(`Method ${req.method} Not Allowed`);
return;
}
//Regular route you need to authenticate the user before opening a compilot user session.
try {
const sessionRes = await apiClient.createSession({
workflowId: process.env.WORKFLOW_ID,
externalCustomerId: preq.session.userId,
});
res.status(200).json(sessionRes);
} catch (error) {
console.error("API call error:", error);
res.status(500).json({ error: "Failed to fetch access token" });
}
}To find the value of
WORKFLOW_ID
see the Workflow detail page.
📄️ Back end
ComPilot recommends that you create your own API using @compilot/js-sdk. There are two things you have to do.
🗃️ Front end
2 items