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
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
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.Create an endpoint returning the result of
createWeb3Challenge
.- Express
- Hono
- Next.js
// 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.// app is your hono app
app.post("/my-api/generate-web3-challenge", async (c) => {
const sessionRes = await apiClient.createWeb3Challenge({
workflowId: WORKFLOW_ID,
...c.body,
});
return c.json(sessionRes);
});To find the value of
WORKFLOW_ID
see the Workflow detail page.import type { NextApiRequest, NextApiResponse } from "next";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const sessionRes = await apiClient.createWeb3Challenge({
workflowId: process.env.WEB3_WORKFLOW_ID,
...req.body,
});
res.status(200).json(sessionRes);
}WEB3_WORKFLOW_ID
is the workflow ID.
Examples
- NextJS Web3 back end
- JavaScript Web3 back end
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" });
}
}
const { createSdk } = require("@compilot/js-sdk");
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const apiClient = createSdk({
// webhookSecret is optional
webhookSecret: process.env.WEBHOOK_SECRET,
apiKey: process.env.API_KEY,
});
const app = express();
const PORT = 5001;
// Middleware
app.use(cors()); // Allows frontend to access the API
app.use(express.json());
//WEB3 route
app.post("/api/generate-web3-challenge", async (req, res) => {
const sessionRes = await apiClient.createWeb3Challenge({
workflowId: process.env.WEB3_WORKFLOW_ID,
...req.body,
});
res.status(200).json(sessionRes);
});
// Start the server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});