Complete Node.js Example
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
npm install @device-park/public-sdk
export DEVICEPARK_CLIENT_ID=your-client-id
export DEVICEPARK_CLIENT_SECRET=your-client-secretSave the following file as device-park-quickstart.mjs:
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("[email protected]")
.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:
node device-park-quickstart.mjsIf 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 until deviceSerial is assigned, and enforce its own maximum wait time.