Configuration
A client needs a Smarter API key and the url of your Smarter platform. Every option of
new Smarter({...}) is optional, because each value is looked up in this order:
the options of
new Smarter({...}),environment variables,
the Smarter CLI’s configuration file,
~/.smarter/config.yaml,the defaults: the
prodenvironment ofplatform.smarter.sh.
This is the lookup of every Smarter SDK, so the same environment configures the Node.js SDK, the Python SDK and the CLI.
import { Smarter } from "@smarter.sh/api";
const client = new Smarter({
apiKey: "your 64-character Smarter api key",
baseUrl: "https://platform.example.com",
timeout: 60, // seconds
maxRetries: 2,
});
API keys
An API key belongs to a user of an account, and the SDK can do whatever that user can do. Create
one in the web console, or with a SmarterAuthToken manifest. See
Authentication. Keep it out of your source code: use an
environment variable or a secret manager. Never ship it to a browser: call the Smarter API from your
own back end.
Environment variables
Variable |
Meaning |
|---|---|
|
The API key. |
|
Your platform’s url, e.g. |
|
|
|
The platform’s root domain, e.g. |
|
Seconds to wait for a response, as in every Smarter SDK, not milliseconds. The default is 60, because a prompt that calls several tools can take tens of seconds. |
|
How many times to retry a request that failed for a transient reason. The default is 2. |
|
|
|
Another path for the CLI’s configuration file. |
Without a base url, the url is derived from the environment, exactly as the CLI derives it:
local is http://localhost:9357, alpha, beta and next are
https://<environment>.<root domain>, and prod is https://<root domain>.
The SDK never reads a .env file itself. Load one the way your application does, e.g. with
node --env-file=.env.
Your own fetch
The SDK sends its requests with Node.js’s built-in fetch, and has no HTTP dependency. Pass your
own fetch to send requests through a corporate proxy, with custom TLS settings, or through a
test double:
import { ProxyAgent, fetch as undiciFetch } from "undici";
import { Smarter } from "@smarter.sh/api";
const dispatcher = new ProxyAgent("http://proxy.example.com:3128");
const client = new Smarter({
fetch: (input, init) => undiciFetch(input, { ...init, dispatcher }),
});
defaultHeaders adds headers to every request. A client holds no connections of its own, so
there is nothing to close.