---
title: Complete Node.js Example
slug: devicepark/sdk-documentation/examples/node-quickstart
description: Run a complete Node.js allocation and session lifecycle.
docTags: 
createdAt: 2026-08-10T08:03:44.631Z
---

This example lists a device, creates an allocation, starts and stops one session, then releases the allocation. It is a complete ES module and always attempts cleanup.

## Prerequisites

```bash
npm install @device-park/public-sdk
export DEVICEPARK_CLIENT_ID=your-client-id
export DEVICEPARK_CLIENT_SECRET=your-client-secret
```

Save the following file as `device-park-quickstart.mjs`:

```javascript
import {
  Credentials,
  DeviceAllocationRequestBuilder,
  DeviceParkApiClient,
  DeviceStartSessionRequestBuilder,
  ListDevicesRequestBuilder
} from "@device-park/public-sdk";

const client = DeviceParkApiClient.builder()
  .url("https://devicepark.testinium.io")
  .credentials(Credentials.fromEnvironment())
  .timeout(60)
  .build();

let allocationId;
let sessionId;

try {
  const devices = await client.devices().list(
    new ListDevicesRequestBuilder().page(0).size(20).build()
  );

  const device = devices.data.find((candidate) => candidate.serial);
  if (!device?.serial) {
    throw new Error("No visible device has a serial number");
  }

  const allocation = await client.allocations().create(
    new DeviceAllocationRequestBuilder()
      .serial(device.serial)
      .priority(3)
      .build()
  );

  if (!allocation.allocationId) {
    throw new Error("Allocation response does not contain allocationId");
  }

  allocationId = allocation.allocationId;

  if (!allocation.deviceSerial) {
    throw new Error(
      `Allocation ${allocationId} is queued at position ${allocation.position ?? "unknown"}`
    );
  }

  const session = await client.sessions().start(
    new DeviceStartSessionRequestBuilder()
      .allocationId(allocationId)
      .userId(42)
      .userEmail("qa@example.com")
      .companyId(7)
      .companyName("Example Company")
      .videoRecording(true)
      .build()
  );

  if (!session.sessionId) {
    throw new Error("Session response does not contain sessionId");
  }

  sessionId = session.sessionId;
  console.log({ allocationId, sessionId, deviceSerial: session.deviceSerial });

  // Run Appium automation here while the session is active.
} finally {
  if (sessionId) {
    try {
      await client.sessions().stop(sessionId);
    } catch (error) {
      console.error(`Could not stop session ${sessionId}`, error);
    }
  }

  if (allocationId) {
    try {
      await client.allocations().delete(allocationId);
    } catch (error) {
      console.error(`Could not release allocation ${allocationId}`, error);
    }
  }

  await client.close();
}
```

Run it:

```bash
node device-park-quickstart.mjs
```

If the allocation enters the queue, this minimal example reports its position and releases it during cleanup. A production runner should keep the same `allocationId`, inspect [List Allocations](docId\:GGobQpsUr2aKNz0jfCNYD) until `deviceSerial` is assigned, and enforce its own maximum wait time.

## Continue

- [Understand allocation targeting and queue behavior](docId\:sPz_N5rutGOmDKrBjbb0L)
- [Review Node.js error types](docId\:nZrw0_UkMOapMnZtgQtMI)
- [Review exact method signatures](docId\:OoaKfd3Ua6tGD-fnw_WuH)
