# Prove your first licensed device (/docs/license-keys/quickstart)

Buy a generated test key, activate one device, and observe `VALID` followed by `INSTANCE_DEACTIVATED`. The paid order issues the key.

Complete [Prepare a test shop](/docs/getting-started/test-shop) in a fresh folder. In admin, follow **Settings > Features > License keys**. Use a generated service and a digital product. Manual `/licenses` creation belongs to list or remote services and is not part of this task.

## Create the service and buy its product [#create-the-service-and-buy-its-product]

Save this as `first-license.mjs` beside `test-shop.mjs`. Continue with the device block in the same file.

```js title="first-license.mjs"
import { randomUUID } from "node:crypto";
import { api, step, buy, request, state, save, suffix, METER, auth } from "./test-shop.mjs";
const service = await step("license-service", "POST", "/license_services", {
  name: `App keys ${suffix}`, label: "License key", type: "generated",
  instructions: "Enter this key in the app.", configuration: { activation_limit: 1, over_limit: "block",
    subscription_grace_days: 3, revoke_on_refund: true, revoke_on_chargeback: true },
});
const product = await step("licensed-product", "POST", "/products", {
  product_id: `licensed-app-${suffix}`, name: "Test licensed app", type: "digital", active: true,
  prices: [{ price: 1, currency: "USD" }], license_service_id: service.license_service_id,
});
const order = await buy("license", product);
let license = order.items.find(i => i.product_id === product.product_id)?.license;
if (!license || license.test !== true || license.source !== "generated") {
  throw new Error("Read the issued generated test license on the paid order.");
}
state.fingerprint ??= randomUUID();
save();
for (let attempt = 0; attempt < 30; attempt++) {
  license = await api("GET", `/licenses/${license.license_id}`);
  if (license.active_in_apollo === true) break;
  if (attempt < 29) await new Promise(resolve => setTimeout(resolve, 1000));
}
if (license.active_in_apollo !== true) {
  throw new Error("License synchronization is pending. Wait, then rerun this file.");
}
console.log({ order: order.order_id, license: license.license_id });
```

In admin, open the paid order and its license. A saved service or product alone does not prove that a key was issued. If payment is unknown, read its state. Never submit it again automatically.

Issued keys reach the usage meter a few seconds after payment. The script reads the issued license up to 30 times, one second apart, and activates only after `active_in_apollo: true`. If synchronization stays pending, keep `guide-state.json` and rerun the same file later. It reuses the original service, product, cart and completed payment, and retains the saved fingerprint. Do not start another purchase to resolve pending synchronization.

## Activate, validate and deactivate the device [#activate-validate-and-deactivate-the-device]

```js title="Observe the device"
const credentials = { account_id: auth.account_id, license_key: license.text };
if (!state.instanceId) {
  const activated = await request(`${METER}/v1/licenses/activate`, "POST", {
    ...credentials, fingerprint: state.fingerprint, instance_name: "Guide test device",
  });
  state.instanceId = activated.instance_id;
  save();
}
const device = { ...credentials, instance_id: state.instanceId };
console.log(await request(`${METER}/v1/licenses/validate`, "POST", device));
console.log(await api("GET", `/licenses/${license.license_id}/instances`));
console.log(await request(`${METER}/v1/licenses/deactivate`, "POST", device));
console.log(await request(`${METER}/v1/licenses/validate`, "POST", device));
```

The first validation is HTTP 200 with `valid: true, code: "VALID"`. After deactivation it is HTTP 200 with `valid: false, code: "INSTANCE_DEACTIVATED"`. Keep those decisions distinct from HTTP errors. In admin, the license's **Devices** view should show the same instance and its deactivation.

The public requests use the account ID and license key, without a secret token or usage key. Save the fingerprint before activation and retain it for retries and later use on this installation. A 503 `try_again` has `Retry-After: 1`; a 429 identifies `license_ip_limit`, `license_key_limit` or `license_account_limit`. Respect the header, then retry the same device identity. For `activation_limit_reached`, read `error.meta.used` and `error.meta.limit`. Invalid fields use `error.meta.field`; safe fingerprint guidance is in `error.message`.

AWS-managed errors can return `{message}` without Retry-After. Use HTTP status and bounded jittered backoff for retryable failures. Unknown-key validation remains HTTP 200 `NOT_FOUND`. See [License key API](/docs/license-keys/license-key-api) for the full decision and error rules. List and remote license behavior is unchanged.

## Try rotation or bring existing keys [#try-rotation-or-bring-existing-keys]

Under **When every device seat is used**, `block` refuses new devices; `rotate` replaces the oldest, imported devices first. Counts stay the same. This affects future keys; [update existing keys](/api/licenses/update-license) separately.

Handle [license:instance_deactivated](/api/webhooks/license-instance-deactivated) once per `event_id`, even with equal `license_version`. [Migrate existing keys](/docs/license-keys/migrate-from-lemon-squeezy), wait for `active_in_apollo: true`, then validate saved UUIDs before new activations.
