# JavaScript SDK

Source: https://timetick.io/docs/developers/sdk-javascript

Run OCPP tests from Node.js or TypeScript with the TimeTick SDK. Install it, start a test on a simulator and read why it failed.

```bash
yarn add @timetick/sdk
```

## Run a test

You need an [API key](https://timetick.io/docs/developers/api-keys), a [simulator](https://timetick.io/docs/simulators/create) and a [test](https://timetick.io/docs/tests/build-a-test). Take the ids from the address bar in the portal.

```ts
import { createClient } from "@timetick/sdk";

const sdk = createClient({ apiKey: process.env.TIMETICK_API_TOKEN });

const projectId = "123";
const testId = "456";
const simulatorId = "789";

const [workspace] = (await sdk.simWorlds.list(projectId)) ?? [];
const test = await sdk.tests.get(testId);
const requirement = test?.requirements?.[0];
if (!workspace?.id || !requirement?.id) throw new Error("Check the ids.");

const run = await sdk.testRuns.create({
  simWorldId: workspace.id,
  scenarioId: testId,
  speedup: 1,
  scheduleMode: "NOW",
  requirementDevices: [
    { requirementId: requirement.id, deviceId: simulatorId, deviceMode: "SIMULATOR" },
  ],
});
if (!run?.id) throw new Error("The run did not start.");

let status: typeof run.status | undefined = run.status;
while (!status || !["COMPLETED", "FAILED", "SKIPPED"].includes(status)) {
  await new Promise((r) => setTimeout(r, 5000));
  status = (await sdk.testRuns.get(run.id))?.status;
}
console.log(status);
```

## See why it failed

```ts
const details = await sdk.testRuns.getDetails(run.id);

const { records } = await sdk.logs.list({
  simulatorId,
  from: new Date(Date.now() - 60 * 60 * 1000),
  to: new Date(),
});
```

`details` has the reason, and `records` has the OCPP messages of the last hour.

The SDK also creates simulators, tests, test plans and Stress Tests. Every method is listed on [npm](https://www.npmjs.com/package/@timetick/sdk).
