Prerequisites

Before you start, make sure you:

1. Full Stack Solution: Use React components to create Full Stack Next.js App

1

Install Coval SDK

Begin by installing the

  • @coval/nextjs for the nextjs specific implementation of the client
  • @coval/react for the embedding the ui components in your project and connecting to the Coval Client
2

Set your Coval API keys

.env.local
NEXT_PUBLIC_COVAL_API_KEY=NEXT_PUBLIC_COVAL_API_KEY
COVAL_API_KEY=YOUR_COVAL_KEY
3

Set up Proxy Path

A proxy path in Next.js is a way to redirect or rewrite API requests to different URLs, typically used to avoid CORS issues or to hide sensitive backend URLs.

Create a proxy path file in your nextjs project under /pages/api/[...proxyPath].ts and add the following code. Feel also free to make other modifications to the proxy code to fit your needs:

[...proxyPath].ts
import { handler } from "@coval/nextjs";
export default handler;
4

Declare the Coval Client

The coval nextjs library needs to be initialized on the client-side using the Next.js ‘use client’ directive. You can do this in the app router or in a separate file.

coval-client.ts
"use client";
import { createClient } from "@coval/nextjs";

if (!process.env.NEXT_PUBLIC_COVAL_API_KEY) {
  throw new Error("Missing API key: NEXT_PUBLIC_COVAL_API_KEY must be set");
}

export const covalClient = createClient({
  apiKey: process.env.NEXT_PUBLIC_COVAL_API_KEY,
  proxyPath: "/api/coval-proxy",
});

export default covalClient;
5

Add <CovalProvider> and Coval Launch Evaluation Component to your app

You can import the CovalProvider component into your app/layout file and wrap your app with it.

Alternatively, here is another example of how you can embed and launch an evaluation.

You can also use the useCoval hook to access the Coval client in your components.

Let’s say you want to create yout own function to fetch metrics and pass them to the LaunchEvalButton component.

Great! Now you can write your own data fetching logic and pass it to the Coval components.