How to create a ClearRun client (createKernelClient)

Call createKernelClient(). It finds your key in hosted.apiKey, then CLEARRUN_API_KEY, then the file saved by clearrun login. Creating the client never throws for a missing key. With no key and no mock option, the first score, evaluate or chat call rejects with MissingApiKeyError. verify and mock mode need no key.

Last updated

Applies to version 1.2.0 of @clearrun/sdk and @clearrun/cli.

Steps

  1. With a key from the environment

    import { createKernelClient } from '@clearrun/sdk';
    
    // reads CLEARRUN_API_KEY
    const client = createKernelClient();
  2. With an explicit key and options

    const client = createKernelClient({
      hosted: {
        apiKey: process.env.CLEARRUN_API_KEY,
        timeoutMs: 15000,
      },
    });
  3. For tests, in mock mode

    const client = createKernelClient({ forceMock: true });

Options

Options
OptionWhat it does
hosted.apiKeyYour key. Sent as Authorization: Bearer
hosted.baseUrlDefault https://api.clearrun.net
hosted.timeoutMsAbort a request after this many milliseconds
forceMockUse mock mode. No network, no key
mock.chatResponseMock chat reply. Passing mock also turns on mock mode
deps.fetchYour own fetch
envWhere to read CLEARRUN_API_KEY and the config path. Default process.env

Methods

score, evaluate, chat and verify. The first three need a key and use credits. verify does not.

It never falls back to mock

If no key is found and you didn't ask for mock mode, the first score, evaluate or chat call rejects with MissingApiKeyError. It won't hand you local test scores by surprise.