Complete Node.js Example
3 min
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 @devicepark/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 import { credentials, deviceallocationrequestbuilder, deviceparkapiclient, devicestartsessionrequestbuilder, listdevicesrequestbuilder } from "@devicepark/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) 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 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 /allocations/list allocations until deviceserial is assigned, and enforce its own maximum wait time continue understand allocation targeting and queue behavior /allocations/create allocationreview node js error types docid\ yncbwpnpb5fms5cblggtjreview exact method signatures /reference/sdk methods
