Skip to main content

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

  1. Install @compilot/js-sdk.

    yarn add @compilot/js-sdk
  2. 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.

  3. Create an endpoint returning the result of createSession. You must pass a customer ID in this API call.

    // 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.