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.createWeb3Challenge() method.

Step by step integration

  1. Install @compilot/js-sdk.

    yarn add @compilot/js-sdk
  2. Import and instantiate createSdk 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 createWeb3Challenge.

    // app is your express app

    app.post("/my-api/generate-web3-challenge", async (req, res) => {
    const sessionRes = await apiClient.createWeb3Challenge({
    workflowId: WORKFLOW_ID,
    ...req.body,
    });
    res.status(200).json(sessionRes);
    });

    To find the value of WORKFLOW_ID see the Workflow detail page.

Examples

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;
}
try {
const sessionRes = await apiClient.createWeb3Challenge({
workflowId: process.env.WORKFLOW_ID,
...req.body,
});
res.status(200).json(sessionRes);
} catch (error) {
console.error("API call error:", error);
res.status(500).json({ error: "Failed to fetch access token" });
}
}