@codewords/client
v0.1.6
Published
Client for interacting with the CodeWords platform using CODEWORDS_API_KEY. Checkout docs.codewords.ai for more information.
Downloads
90
Readme
CodeWords Client
A TypeScript client for interacting with the CodeWords runtime API.
Usage
Basic Usage
import { createServiceClient } from "@codewords/client";
// Create a client instance with a standard API key or one-time API key
const client = createServiceClient("cwk-your-api-key");
const client = createServiceClient("cwotk-your-one-time-key");
// Run a service
async function runExample() {
const result = await client.runService("service-id", "path", {
// request body
key: "value",
});
console.log(result);
}API Key Types
The client supports two types of API keys:
- Standard API Keys (prefix:
cwk-): Persistent keys that can be used multiple times - One-Time API Keys (prefix:
cwotk-): Keys that are invalidated after a single use
// Using a standard API key
const standardClient = createServiceClient("cwk-your-api-key");
// Using a one-time API key
const oneTimeClient = createServiceClient("cwotk-your-one-time-key");
// Check the type of key being used
import { ApiKeyType } from "@codewords/client";
if (client.getApiKeyType() === ApiKeyType.ONE_TIME) {
console.log("Using a one-time key that will be invalidated after use");
}Streaming Logs
You'd need to have started a run before you can stream logs.
// Stream logs for a request
for await (const logLine of client.streamLogs(request_id)) {
console.log(logLine);
}File Uploads
// Method 1: Create upload/download URLs
const { upload_uri, download_uri } =
await client.createFileUpload("example.txt");
await client.uploadFile(upload_uri, Buffer.from("Hello, world!"));
console.log("File available at:", download_uri);
// Method 2: Simplified helper method
const downloadUrl = await client.uploadFileAndGetUrl(
"example.txt",
Buffer.from("Hello, world!")
);
console.log("File available at:", downloadUrl);Getting Request Details
// Get details for a completed request
const details = await client.getRequestDetails(request_id);
console.log("Request completed at:", details.completedAt);
console.log("Response:", details.responseContent);API Reference
Constructor
new ServiceClient(apiKey: string)apiKey: Your API key for authentication (can be a standard key 'cwk-' or one-time key 'cwotk-')
Methods
getApiKeyType(): ApiKeyType- Get the type of API key being usedrunService<T>(serviceId: string, path?: string, body?: any, requestId?: string, correlationId?: string, queryParams?: string): Promise<T>streamLogs(requestId: string): AsyncGenerator<string>createFileUpload(filename: string): Promise<UploadDownloadLinks>uploadFile(uploadUrl: string, file: any): Promise<AxiosResponse>provisionRequest(): Promise<ProvisionedRequestResponse>getRequestDetails(requestId: string): Promise<RequestDetails>uploadFileAndGetUrl(filename: string, file: any): Promise<string>
Types
enum ApiKeyType {
STANDARD = "STANDARD", // Regular API key (cwk-)
ONE_TIME = "ONE_TIME", // One-time API key (cwotk-)
UNKNOWN = "UNKNOWN", // Unknown key format
}
interface UploadDownloadLinks {
upload_uri: string;
download_uri: string;
}
interface ProvisionedRequestResponse {
request_id: string;
}
interface RequestDetails {
id: string;
userId: string;
serviceId?: string;
createdAt: string;
startedAt?: string;
completedAt?: string;
responseStatus?: number;
responseContent?: string;
responseJson?: any;
}