@chalk-ai/client

TypeScript client for Chalk.
Installation
$ yarn add @chalk-ai/client
Generate Types
Starting in your Chalk project directory, run:
$ chalk codegen typescript --out=generated_types.ts
Usage
Modern JavaScript ES6
import { ChalkGRPCClient } from "@chalk-ai/client"
import { FeaturesType } from "local/generated_types";
interface FeaturesType {
"user.id": string;
"user.fraud_score": number;
}
const client = new ChalkGRPCClient<FeaturesType>();
const result = await client.query({
inputs: {
"user.id": "1",
},
outputs: ["user.fraud_score"],
});
console.log(result.data["user.fraud_score"].value);
CommonJS
var ChalkGRPCClient = require("@chalk-ai/client").ChalkGRPCClient;
interface FeaturesType {
"user.id": string;
"user.fraud_score": number;
}
var client = new ChalkGRPCClient<FeaturesType>();
client
.query({
inputs: {
"user.id": "1",
},
outputs: ["user.fraud_score"],
})
.then((result) => {
console.log(result.data["user.fraud_score"].value);
});
Constructor options (gRPC)
See the HTTP Options if you are using the HTTP Client, but we encourage
reading the migration guide.
import { ChalkGRPCClient } from "@chalk-ai/client"
const options: ChalkGRPCClientOpts = { };
const chalkClient = new ChalkGRPCClient(options);
export interface ChalkGRPCClientOpts {
clientId?: string;
clientSecret?: string;
apiServer?: string;
activeEnvironment?: string;
fetch?: CustomFetchClient;
fetchHeaders?: typeof Headers;
timestampFormat?: TimestampFormat.ISO_8601 | TimestampFormat.EPOCH_MILLIS;
skipQueryServerFromCredentialExchange?: boolean;
grpcClientOptions?: Partial<ClientOptions>;
}
Migrating to the gRPC Client
While Chalk supports both an HTTP Client and a gRPC Client for interfacing with Chalk, we heavily encourage users to use
the gRPC Client for better performance, as this targets a more performant gRPC Query Server. After setting up a gRPC
Query Server in your Chalk Environment, changing to the gRPC Client can be done in a few steps:
- Use the import
ChalkGRPCClient instead of ChalkClient
- Check to see if any of the following initialization options need to be changed:
- The option
useQueryServerFromCredentialExchange has been changed to skipQueryServerFromCredentialExchange
and negated to better reflect the default behavior for the gRPC Query Server.
- Check your Chalk Environment for any custom routing rules. If you are not sure, reach out to Chalk support for help here.
- If your previous SDK client explicitly sets the
engine-type: engine header on requests, this will need to be changed
to engine-grpc (or omitted, the gRPC client will automatically set this header but respects all overrides).
- It is most likely not necessary to change a provided
QueryServer as most routing is done via SDK-set headers, but
depending on your setup this may need to be directly specified if using a non-standard port - the gRPC query server listens
on port 443 by default.
- It is not necessary to remove the
fetch or fetchHeaders as these are still used during some HTTP routines
such as fetching credentials.
After these initialization changes are made, no changes to actual calls should be necessary - the gRPC Client uses the
same function signatures.
At this moment, only query(), queryBulk(), and multiQuery() are supported - if your usage requires one of the other
functions (whoami(), triggerResolverRun(), uploadSingle(), getRunStatus()), the legacy ChalkClient should be used
(but ideally only for these four calls - we still strongly recommend partially migrating the query calls to the gRPC Client).
Supported environment variables
process.env._CHALK_CLIENT_ID | Required | Your Chalk client ID. You must specify this environment variable or pass an explicit clientId value when constructing your ChalkClient |
process.env._CHALK_CLIENT_SECRET | Required | Your Chalk client secret. You must specify this environment variable or pass an explicit clientSecret value when constructing your ChalkClient |
process.env._CHALK_ACTIVE_ENVIRONMENT | Optional | The environment that your client should connect to. If not specified, the client will query your project's default environment |
process.env._CHALK_API_SERVER | Optional | The API server that the client will communicate with. This defaults to https://api.chalk.ai which should be sufficient for most consumers |
You can find relevant variables to use with your Chalk Client by
running chalk config with the Chalk command line tool.
$ chalk config
Path: ~~~
Name: Organization Token
Client Id: <client-id>
Client Secret: <client-secret>
Environment: <active-environment>
API Server: https://api.chalk.ai
Valid Until: 2023-11-10T06:11:17.516000
Constructor options (Legacy)
import { ChalkCLient } from "@chalk-ai/client"
const options: ChalkClientOpts = {};
const chalkClient = new ChalkClient(opts);
export interface ChalkClientOpts {
clientId?: string;
clientSecret?: string;
apiServer?: string;
activeEnvironment?: string;
fetch?: CustomFetchClient;
fetchHeaders?: typeof Headers;
timestampFormat?: TimestampFormat.ISO_8601 | TimestampFormat.EPOCH_MILLIS;
useQueryServerFromCredentialExchange?: boolean;
}