# Move an app to a new device

> A local-first app with no accounts. A code on the old phone, typed into the new one, and the whole database follows. How to build it with s3nd, what to watch, and the full guide.

Canonical: https://www.s3nd.sh/use-cases/new-device · Markdown: https://www.s3nd.sh/use-cases/new-device.md · Français: https://www.s3nd.sh/fr/use-cases/new-device

A local-first app with no accounts. A code on the old phone, typed into the new one, and the whole database follows.

```ts
import { store, SCHEMA_VERSION } from '@/lib/store'

export async function POST(request: Request) {
  const state = await request.json()
  const code = store.codes.create()

  await store.putSnapshot(code, state, {
    app: 'notes',
    version: SCHEMA_VERSION,
    device: request.headers.get('user-agent') ?? undefined,
    expiresIn: 60 * 60,
    ifAbsent: true,
  })

  return Response.json({ code })
}
```

## What is actually going on.
### The problem
The user has a new phone. The app on the old one holds everything, in IndexedDB, and there is no account to sign into because the app never needed one. IndexedDB does not leave the browser it was written in.

### What s3nd does about it
The old device dumps its object stores, posts them to your API, and gets an eight-character code back. The new device types the code, sees what it is about to restore, and replaces its empty database in one transaction. The snapshot expires within the hour and the code is burned on success.

## The things that are easy to get wrong.
- **Confirm before replacing** — Return createdAt and device from the lookup, and show them. The most common mistake is restoring onto the device that already had the data.
- **Version the snapshot** — Pass maxVersion on read. A snapshot from a newer build of the app fails with a clear error instead of landing in a version that will misread it.
- **Burn the code** — A DELETE after a successful import is the tidy path; the expiry is the backstop. Both are one line.

Packages: s3nd, @s3nd/react. [Move to a new device, in full](https://doc.s3nd.sh/docs/use-cases/new-device)
