# Publish an APK to the WorldStreet Store

This guide is written so an AI agent or a CI job can follow it from start to finish without help. A person only needs to do the one-time setup.

Store: https://store.worldstreetgold.com
API base: https://store.worldstreetgold.com/api/v1/publisher

## One-time setup (a person does this)

1. **Create the app** in the console: https://store.worldstreetgold.com/console → New app. The Android package name (for example `com.worldstreetgold.crypto`) can't be changed later, and every APK must match it.
2. **Create a CI key**: the app → CI keys → New key. Tick `upload`, and also `publish_beta` if the agent should publish to the beta or internal track. The key starts with `ws_` and is shown once.
3. **Copy the app id**: the app → Settings → "App id (for the CLI)". It's a UUID.
4. **Hand these to the agent as environment variables**, not in chat or in files:

```bash
export WORLDSTORE_KEY="ws_..."            # the CI key
export WORLDSTORE_APP_ID="00000000-0000-0000-0000-000000000000"
export WORLDSTORE_URL="https://store.worldstreetgold.com"
```

5. **After the first upload**, open the app's Settings and pin the signing certificate. From then on, an APK signed with any other key is rejected.

## Rules for the agent

Read these before doing anything.

- **Never print, log or commit `WORLDSTORE_KEY`.** Read it from the environment only.
- **Upload a signed release `.apk`.** An `.aab` bundle won't work. A debug build can only go on the `internal` track.
- **Use the right signature.** The APK must be signed with signature scheme v2 or v3, using the same keystore as earlier releases.
- **`versionCode` must be higher** than every version already uploaded for this app. Bump it before building.
- **The package name must match** the app's package name exactly.
- **The file limit is 300 MB.**
- **Default to the `beta` track.** A key can never publish to `stable`: a person does that in the console. If you're asked for stable, upload to stable but don't publish, and give the person the release link.
- **A failed release can't be retried with the same file.** Fix the cause, rebuild, and start a new release.
- **There's a rate limit** of 10 new releases per app per hour.
- **Stop and ask the person** if the key is rejected (401 or 403), the package name doesn't match, or the signing key differs. Don't try to work around these.

## Before uploading: preflight checks

These need the Android build tools (`aapt2` or `apksigner`) on PATH. Skip any you can't run, and the store will catch the problem anyway.

```bash
APK=app/build/outputs/apk/release/app-release.apk

# Package name, versionCode and versionName
aapt2 dump badging "$APK" | head -1
# expect: package: name='<the app's package name>' versionCode='<higher than before>' versionName='...'

# Signed with v2 or v3, not debug
apksigner verify --print-certs -v "$APK" | grep -E "Verified using v[23] scheme|Signer #1 certificate (DN|SHA-256)"

# What the store expects for this app
curl -fsS -H "Authorization: Bearer $WORLDSTORE_KEY" "$WORLDSTORE_URL/api/v1/publisher/apps/$WORLDSTORE_APP_ID"
# → {"id":"...","slug":"...","packageName":"com.worldstreetgold.crypto","name":"..."}
```

Building with Expo? Use an EAS profile that outputs an APK:

```json
{ "build": { "production-apk": { "android": { "buildType": "apk" } } } }
```

```bash
eas build -p android --profile production-apk
```

## Option A: the CLI (recommended)

Node 20 or newer. No install needed.

```bash
curl -fsSLO "$WORLDSTORE_URL/cli/worldstore.mjs"

# Upload and verify only (a person publishes it in the console)
node worldstore.mjs publish "$APK" --app "$WORLDSTORE_APP_ID" --track beta --notes "What's new in this build"

# Upload, verify and publish to beta for everyone
node worldstore.mjs publish "$APK" --app "$WORLDSTORE_APP_ID" --track beta \
  --notes-file CHANGELOG.md --publish --rollout 100
```

Progress goes to stderr. The last line on stdout is JSON:

```json
{"releaseId":"…","status":"published","versionCode":42,"sha256":"…"}
```

On failure it exits non-zero with `✖ <reason>`. Look the reason up in the failure table below.

Other flags:
- `--rollout 10`: publish to 10% of devices first.
- `--min-supported <versionCode>`: force every device older than this version to update.
- `--icon icon.png`: also change the app's store icon (square PNG, JPEG or WebP, at least 192×192). It changes right away, before the release is even verified.

To change only the icon, with no release: `node worldstore.mjs icon icon.png --app "$WORLDSTORE_APP_ID"`.

## Option B: plain HTTP (curl)

Use this when Node isn't available. All requests send `Authorization: Bearer $WORLDSTORE_KEY`. Every POST should also send an `Idempotency-Key` header (any new UUID), so a retried request isn't applied twice.

### 1. Create the release

```bash
curl -fsS -X POST "$WORLDSTORE_URL/api/v1/publisher/apps/$WORLDSTORE_APP_ID/releases" \
  -H "Authorization: Bearer $WORLDSTORE_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{"track":"beta","notesMd":"What changed, in Markdown","fileName":"app-release.apk"}'
```

`track` is `beta`, `internal` or `stable`. Response (201):

```json
{
  "releaseId": "…",
  "uploadUrl": "https://store.worldstreetgold.com/api/v1/publisher/releases/<releaseId>/file",
  "statusUrl": "https://store.worldstreetgold.com/api/v1/publisher/releases/<releaseId>",
  "maxBytes": 314572800
}
```

### 2. Upload the file

Send the raw APK bytes as the body, not a multipart form:

```bash
curl -fsS -X PUT "$UPLOAD_URL" \
  -H "Authorization: Bearer $WORLDSTORE_KEY" \
  -H "Content-Type: application/vnd.android.package-archive" \
  --data-binary @"$APK"
```

Response (202): `{"releaseId":"…","status":"verifying","bytes":12345678}`

Optional, at any time: change the app's store icon. It isn't tied to a release and shows straight away:

```bash
curl -fsS -X POST "$WORLDSTORE_URL/api/v1/publisher/apps/$WORLDSTORE_APP_ID/icon" \
  -H "Authorization: Bearer $WORLDSTORE_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -F "file=@icon.png"
```

### 3. Wait for verification

The store checks the signature, reads the version and permissions, and scans for malware. This usually takes 10 to 60 seconds. Poll every 3 to 5 seconds, for up to 15 minutes:

```bash
curl -fsS "$STATUS_URL" -H "Authorization: Bearer $WORLDSTORE_KEY"
```

```json
{
  "id": "…", "status": "ready", "track": "beta",
  "versionCode": 42, "versionName": "1.4.0",
  "failureReason": null, "rollout": 0,
  "artifact": { "sha256": "…", "sizeBytes": 12345678, "certSha256": ["…"], "scan": "clean", "permissions": ["android.permission.INTERNET"] }
}
```

Keep polling while `status` is `draft` or `verifying`. When it's `ready`, go on to the next step. If it's `failed`, read `failureReason`, then stop and report.

### 4. Publish (optional; beta and internal only)

```bash
curl -fsS -X POST "$WORLDSTORE_URL/api/v1/publisher/releases/$RELEASE_ID/publish" \
  -H "Authorization: Bearer $WORLDSTORE_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{"rolloutPercentage":100,"minSupportedVersionCode":null}'
```

The key needs the `publish_beta` scope. The response includes `release`, `approvalNeeded` and `why`. If `approvalNeeded` is true, a second person has to approve in the console. Tell the person what `why` says.

### 5. Report back to the person

Include:
- the version name and code,
- the track,
- the status (`ready`, `published` or `awaiting_approval`),
- the rollout percentage,
- the SHA-256,
- any new permissions,
- the console link: `https://store.worldstreetgold.com/console/apps/<slug>/releases/<releaseId>`. The slug comes from the app info call in the preflight step.

## Release statuses

| Status | Meaning | Next step |
|---|---|---|
| `draft` | Created, but the file hasn't arrived yet | Upload the file |
| `verifying` | Checks are running | Keep polling |
| `ready` | Passed the checks, not visible to users | Publish (beta and internal), or a person publishes (stable) |
| `failed` | Rejected, see `failureReason` | Fix it, rebuild, start a new release |
| `awaiting_approval` | Needs a second person | Wait, tell the person |
| `published` | Live for the rollout percentage | Done |
| `halted` | Rollout paused by a person | Nothing; a person resumes it |
| `withdrawn` | Pulled by a person | Nothing |

## Failure reasons and fixes

| `failureReason` contains | Fix |
|---|---|
| `package is X, but this app is Y` | Wrong app id, or the wrong build flavour. Stop and ask. |
| `Version code N must be higher than M` | Bump `versionCode` above M, then rebuild. |
| `signature doesn't verify` | The APK was changed after signing. Rebuild and sign again. |
| `signature scheme v2 or v3` | Turn on v2 signing (`enableV2Signing true`), then rebuild. |
| `different key than this app's earlier releases` | Wrong keystore. Stop and ask. Never re-sign with a new key. |
| `debug build` | Build the release variant, or use `--track internal`. |
| `larger than … MB` | Shrink the APK: enable R8/minify, split by ABI. |
| `doesn't look like a valid APK` | You uploaded an `.aab`, a zip or a broken file. Upload the `.apk`. |
| `malware scan flagged` | Stop and tell the person. Don't retry. |
| `malware scan couldn't run` | Server problem. Wait a few minutes, then start a new release. |
| `upload never arrived` | The PUT didn't finish. Start a new release. |

## HTTP errors

Errors come back as JSON:

```json
{ "error": { "code": "FORBIDDEN", "message": "This key lacks the \"publish_beta\" scope" } }
```

| Status | Meaning |
|---|---|
| 400 | Bad input. `message` names the field. |
| 401 | The key is missing, wrong or revoked. Stop and ask. |
| 403 | The key lacks a scope or belongs to another app. Stop and ask. |
| 404 | Wrong app id or release id. |
| 409 | That release already has its file. Start a new release. |
| 413 | The file is too large. |
| 429 | Rate limited. Wait and retry later. |
| 5xx | Retry with backoff, at most 3 times. |

## A prompt you can give your AI

> Read https://store.worldstreetgold.com/docs/publish-apk.md and follow it. Build a signed release APK of this project with a higher versionCode, run the preflight checks, then publish it to the WorldStreet Store on the beta track at 100% with release notes summarising the commits since the last tag. WORLDSTORE_KEY, WORLDSTORE_APP_ID and WORLDSTORE_URL are already set in the environment. Report the release link when it's done.
