# Ship an over-the-air update (Expo)

An over-the-air (OTA) update changes an app's JavaScript and assets without a new APK. Use it for JS-only changes. If you changed native code or a native dependency, or bumped the Expo SDK, you need a new APK instead: see https://store.worldstreetgold.com/docs/publish-apk.md.

Store: https://store.worldstreetgold.com

## One-time setup in the app

1. In the console, open the app → **Updates**. Copy the `app.json` snippet shown there. It looks like this:

```json
{
  "expo": {
    "runtimeVersion": { "policy": "appVersion" },
    "updates": {
      "url": "https://store.worldstreetgold.com/api/ota/<slug>/manifest",
      "requestHeaders": { "expo-channel-name": "production" },
      "codeSigningCertificate": "./certs/certificate.pem",
      "codeSigningMetadata": { "keyid": "main", "alg": "rsa-v1_5-sha256" }
    }
  }
}
```

2. Generate a code-signing key pair once, and keep `keys/` out of git:

```bash
npx expo-updates codesigning:generate --key-output-directory keys --certificate-output-directory certs --certificate-validity-duration-years 10 --certificate-common-name "WorldStreet"
```

3. Build and publish an APK with this config. Only APKs built with it can receive OTA updates.
4. Create a CI key with the `ota` scope: the app → CI keys.

## Publishing an update (agent steps)

Rules:
- `--runtime-version` must equal the `runtimeVersion` built into the installed APK. With the `appVersion` policy, that's the app's `version`, for example `1.4.0`. If it doesn't match, devices ignore the update.
- Always sign with `--private-key`. Apps with a code-signing certificate refuse unsigned updates.
- Start with a small rollout, then raise it. The store halts a rollout automatically if the crash rate jumps.

```bash
npx expo export --platform android
curl -fsSLO "$WORLDSTORE_URL/cli/worldstore.mjs"
node worldstore.mjs ota publish --app "$WORLDSTORE_APP_ID" \
  --runtime-version 1.4.0 --dist dist --channel production \
  --private-key keys/private-key.pem --message "Fix login button" --rollout 10
```

The CLI uploads only assets the store doesn't already have, signs the manifest, and prints the update as JSON on stdout.

## Managing a rollout

All of these need a key with the `ota` scope, or a signed-in person:

| Action | Request |
|---|---|
| Change the rollout | `POST /api/v1/publisher/ota/<updateId>/rollout` `{"percentage": 50}` |
| Roll back | `POST /api/v1/publisher/ota/<updateId>/rollback` `{"reason": "Crash on launch"}` |
| Restore | `POST /api/v1/publisher/ota/<updateId>/restore` `{"reason": "Fixed server side"}` |
| Promote to another channel | `POST /api/v1/publisher/ota/<updateId>/promote` `{"toChannel": "production", "rolloutPercentage": 5}` |

If something is badly wrong, the console's Updates tab has **Emergency: use built-in bundle**. It sends every device back to the JavaScript inside its APK.
