Installation and first scan
Install the client, configure an API key, discover your organization, and launch a blackbox scan.
Install
Install the released package from the registry configured for your application:
npm install @straylabs/sdk
# or
bun add @straylabs/sdkIf the package release is not available in your registry yet, build a tarball from the SDK repository:
# In the SDK checkout
bun install --frozen-lockfile
bun run build
bun pm pack
# In your server application; use the tarball path printed above
npm install /path/to/straylabs-sdk-0.1.0.tgzUse Node.js 22+ or Bun 1.3+. TypeScript users can import the public types alongside the client. The SDK is intended for server applications, functions, workers, and scripts.
Configure the client
import { StraylabsClient } from "@straylabs/sdk";
const apiKey = process.env.STRAYLABS_API_KEY;
if (!apiKey) throw new Error("STRAYLABS_API_KEY is required");
const client = new StraylabsClient({ apiKey });| Option | Default | Purpose |
|---|---|---|
apiKey | Required | Opaque API key sent through x-api-key |
baseUrl | https://platform.straylabs.ai/api | Optional API-root override, including its deployment prefix |
timeoutMs | 30_000 | Whole operation, including read retries, delays, and response-body reads |
maxReadRetries | 2 | Additional GET attempts after the first; POST is never automatically retried |
fetch | Native fetch | Optional fetch implementation, including a mock for tests |
const localClient = new StraylabsClient({
apiKey,
baseUrl: "http://localhost:8000",
timeoutMs: 60_000,
maxReadRetries: 1,
});Overrides require an absolute HTTPS URL, except HTTP on loopback development addresses. Credentials, query strings, and fragments are rejected in the base URL. The client preserves prefixes such as /api and rejects authenticated redirects. A custom fetch must honor the request's abort signal and redirect policy.
Create and manage API keys in Platform account settings. Keys inherit their owning user's access and can expire or be revoked. The SDK does not perform interactive sign-in, accept agreements, or provision organizations.
Resolve the organization through the API
const organization = await client.organizations.getByName("Acme Corp");Names match exactly and are case-sensitive. If no name matches, the SDK throws StraylabsOrganizationLookupError with reason: "not_found". Duplicate names produce reason: "ambiguous". To discover or select an organization by ID:
const organizations = await client.organizations.list();
const selected = organizations.find((entry) => entry.id === selectedOrganizationId);
if (!selected) throw new Error("The selected organization is not accessible");selectedOrganizationId is the value selected from a previous API result by your application or operator. Keep that selection associated with the intended customer when your key accesses several organizations.
Create a project and start a scan
This example starts a real scan when run. Use targets you are authorized to test.
const project = await client.projects.createBlackbox(organization.id, {
project_name: "customer-request-456",
targets: ["https://app.customer.example"],
scope: "Public web application and API within the listed target.",
exclusions: "Payments and load testing.",
environment: "staging",
});
const projectRef = {
organizationId: organization.id,
projectId: project.id,
};
// Persist projectRef and this history snapshot before sending the start request.
const beforeStart = await client.scans.list(projectRef);
const started = await client.scans.start(projectRef);
if (!started.current_scan_id) {
throw new Error("Reconcile scan history before attempting another start");
}
const scanRef = { ...projectRef, scanId: started.current_scan_id };
// Persist scanRef immediately so later refreshes can track this execution.Creation and start are separate POST requests. A successful start returns ProjectOut; its id is the project ID and current_scan_id is the scan ID. The project pointer later becomes null, so keep the saved scan ID.
Persist a launch intent before each write, the pre-start scan IDs, and every successful checkpoint. If a mutation response is lost, use recovery reads rather than sending the POST again blindly. A deterministic project name helps reconciliation but is not an idempotency key.
Refresh later
const scan = await client.scans.get(scanRef);
console.log({
progress: scan.progress_percent,
status: scan.status,
failed: scan.status === "failed",
});The call returns current details immediately. Your application decides when to refresh again. Continue with scan history and latest-scan lookup, report generation and download, and error handling.
Last updated