# Upload new version

Send a new package of a live item, then complete it. We check the size and SHA-256 of the whole file and submit it like the Workspace form: it waits for review (one pending update per item) unless your account is trusted, in which case it goes live and the old package is removed. Keep your own copies of old versions. Needs a token with `items:upload`.

## How it works
1. **Start an upload.** Declare the file name, size and SHA-256, and choose `transfer: "direct"`.
2. **Send the parts.** Ask for a signed URL per part and PUT the bytes straight to storage — fast from anywhere.
3. **Complete.** We check every part and the SHA-256; the new version then waits for review, or goes live for trusted authors.
There are two ways to send the bytes:

- **`transfer=direct`** — recommended. Each part goes straight to our storage with a short-lived URL we sign for you: fast from anywhere, no 100-second limit. Parts are 5–100 MB (default 50 MB; the last one is the remainder).
- **`transfer=relay`** — the default for older scripts: chunks go through our servers (1–50 MB each). Every chunk must arrive within 100 seconds, which can be slow from far away.

> **Tip:** From a slow or distant network, direct can be a hundred times faster than relay — measured from Vietnam: about 30 KB/s through our servers, 4 MB/s direct.

## 1. Start an upload

`POST /api/items/{id}/uploads`

| Field | Type | Description |
|---|---|---|
| `filename` *required* | string | Package file name, e.g. `my-item-2.5.0.zip` |
| `size` *required* | integer | Bytes |
| `sha256` *required* | hex | Checksum of the whole file |
| `transfer` | enum | `direct` (recommended) or `relay` (default) |
| `chunk_size` | integer | Bytes per part — direct 5–100 MB, relay 1–50 MB; default 50 MB |
| `slot` | enum | `main_file` (default) or `installable_file` (WordPress items, zip) |
| `replace_pending` | boolean | `true` when an update of this item is already waiting for review: this upload replaces its package (and the version and changelog you complete it with). See [Hotfixes](https://sellmycode.net/api-docs/publishing.md#hotfixes-while-an-update-waits) |
```bash
curl -X POST https://sellmycode.net/api/items/123/uploads \
  -H "Authorization: Bearer $(cat ~/.config/sellmycode/token)" \
  -H "Content-Type: application/json" \
  -d '{"filename":"my-item-2.5.0.zip","size":48211904,"sha256":"9f2c…e1","transfer":"direct"}'
```
```php
$res = (new GuzzleHttp\Client)->post('https://sellmycode.net/api/items/123/uploads', [
    'headers' => ['Authorization' => 'Bearer ' . $token],
    'json' => ['filename' => basename($file), 'size' => filesize($file),
               'sha256' => hash_file('sha256', $file), 'transfer' => 'direct'],
]);
```
```node
const res = await fetch("https://sellmycode.net/api/items/123/uploads", {
  method: "POST",
  headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
  body: JSON.stringify({ filename, size, sha256, transfer: "direct" }),
});
```
```python
res = requests.post("https://sellmycode.net/api/items/123/uploads",
    headers={"Authorization": f"Bearer {token}"},
    json={"filename": name, "size": size, "sha256": sha, "transfer": "direct"})
```
```json
{
  "status": "success",
  "upload": { "id": "0d049a6c-…-2cda", "transfer": "direct", "chunk_size": 52428800,
              "chunk_count": 1, "received": 0, "missing": [0], "expires_at": "2026-10-24T14:00:00Z" }
}
```
You can have 2 unfinished uploads at a time; an upload expires after 6 hours.

## 2. Send the parts

### Direct

`GET /api/items/{id}/uploads/{upload}/parts/{index}`

Returns a signed URL for part `index` (0 … chunk_count − 1), valid for 60 minutes. PUT exactly that part to it — **no Authorization header**, the URL is the permission. Ask again if it expires.

```json
{ "status": "success", "index": 0, "method": "PUT", "url": "https://…r2.cloudflarestorage.com/…",
  "headers": { "Content-Length": 52428800 }, "expires_at": "2026-10-24T09:00:00Z" }
```

> **Warning:** A part larger than planned cancels the whole upload and counts as a failed upload.

### Relay

`PUT /api/items/{id}/uploads/{upload}/chunks/{index}`

Raw request body, exactly `chunk_size` bytes (the last chunk is the remainder). The optional header `X-Chunk-Sha256` checks the chunk. Re-sending a chunk replaces it.

## 3. Check progress

`GET /api/items/{id}/uploads/{upload}`

Shows how many parts arrived and which are missing — use it to resume after a network error.

## 4. Complete

`POST /api/items/{id}/uploads/{upload}/complete`

| Field | Type | Description |
|---|---|---|
| `version` | string | e.g. 2.5.0 — an installable package uses its own Version header when omitted |
| `changelog` | string | Shown to buyers when the version goes live |

```json
{ "status": "success", "item_id": 123, "mode": "review", "changed": ["main_file", "version", "changelog"],
  "update_id": 846, "replaced_pending": false, "sha256": "9f2c…e1", "version": "2.5.0" }
```

## List and cancel uploads

`GET /api/uploads`

`DELETE /api/items/{id}/uploads/{upload}`

List your unfinished uploads (all items) and cancel one you will not finish — it frees the slot at once and does not count as a failed upload.

## A complete release script
```bash
F=my-item-2.5.0.zip; ID=123; API=https://sellmycode.net/api; AUTH="Authorization: Bearer $(cat ~/.config/sellmycode/token)"
SIZE=$(stat -f%z "$F" 2>/dev/null || stat -c%s "$F"); SHA=$(shasum -a 256 "$F" | cut -d' ' -f1); CS=52428800
J=$(curl -s -X POST $API/items/$ID/uploads -H "$AUTH" -H "Content-Type: application/json" \
  -d "{\"filename\":\"$F\",\"size\":$SIZE,\"sha256\":\"$SHA\",\"transfer\":\"direct\",\"chunk_size\":$CS}")
UP=$(echo "$J" | python3 -c 'import sys,json;print(json.load(sys.stdin)["upload"]["id"])')
N=$(echo "$J" | python3 -c 'import sys,json;print(json.load(sys.stdin)["upload"]["chunk_count"])')
for i in $(seq 0 $((N-1))); do
  URL=$(curl -s $API/items/$ID/uploads/$UP/parts/$i -H "$AUTH" | python3 -c 'import sys,json;print(json.load(sys.stdin)["url"])')
  dd if="$F" bs=$CS skip=$i count=1 2>/dev/null | curl -sf -X PUT --data-binary @- "$URL" > /dev/null || echo "part $i failed — run it again"
done
curl -s -X POST $API/items/$ID/uploads/$UP/complete -H "$AUTH" -H "Content-Type: application/json" \
  -d '{"version":"2.5.0","changelog":"Fixed checkout on PHP 8.4"}'
```
## Errors

| Code | HTTP | Meaning |
|---|---|---|
| `pending_update` | 409 | Another update of this item is waiting for review — wait, or start the upload with `replace_pending: true` |
| `incomplete` | 409 | Parts are missing — the message lists them |
| `checksum_mismatch` | 422 | The file differs from the declared size or SHA-256 — start a new upload |
| `oversized_part` | 422 | A part was larger than planned; the upload was cancelled |
| `chunk_size` | 422 | The part size is outside the allowed range |
| `file_type` | 422 | The item category does not accept this file type |
| `not_installable` | 422 | No WordPress theme or plugin with a Version header in the zip |
| `too_many_uploads` | 429 | Finish or cancel your other uploads first |
| `daily_limit` | 429 | 10 GB per author per 24 hours |
| `upload_locked` | 429 | Five failed or abandoned uploads in 24 hours pause uploads for a day |
| `busy` | 503 | Upload capacity is busy — try again in a few minutes |

---
Source: https://sellmycode.net/api-docs/upload
