> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agg.market/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> To integrate AGG, start with Quickstart: REST (https://docs.agg.market/quickstart/rest), then Order lifecycle & statuses (https://docs.agg.market/concepts/order-lifecycle).
> Track every trade until it reaches a terminal status. Before retrying a failed or timed-out call, read Errors, retries & idempotency (https://docs.agg.market/concepts/errors).
> The API reference is generated from https://docs.agg.market/openapi/openapi.json.

# Avatar Upload

> Upload and set a user avatar with presigned S3 URLs

> [!NOTE]
>
> Avatar endpoints require a signed-in user session. See [Authentication](https://docs.agg.market/recipes/authentication.md)
> for the initial sign-in flow.

Avatar upload is a two-step process: request a presigned upload URL, then upload the file directly
to S3. The URL is valid for 10 minutes. After upload, the image is processed to WebP. Confirm with
`client.updateUser({ confirmAvatar: true })` (`PATCH /users/me`), or poll until `avatarUrl` is on
the profile.

## Step 1: Get Upload URL

`contentType` is `image/jpeg`, `image/png`, `image/gif`, or `image/webp`.

```ts SDK theme={null}
const contentType = "image/jpeg";
const { uploadUrl, assetKey, cdnUrl } = await client.createAvatarUploadUrl(contentType);
```

```bash cURL theme={null}
curl -X POST https://api.agg.market/users/me/avatar \
  -H "x-app-id: $AGG_APP_ID" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "content-type: application/json" \
  -d '{ "contentType": "image/jpeg" }'
```

[Get avatar upload URL](https://docs.agg.market/api-reference/users/get-avatar-upload-url.md).

## Step 2: Upload the Image

Upload the raw file bytes directly to the presigned URL. The `Content-Type` on the S3 PUT must be
the type you presigned.

```ts theme={null}
const file = inputElement.files[0];

await fetch(uploadUrl, {
  method: "PUT",
  headers: { "Content-Type": contentType },
  body: file,
});
```

## Step 3: Confirm

Confirming is `PATCH /users/me` with `{ "confirmAvatar": true }`, which is
`client.updateUser({ confirmAvatar: true })`. Retry while the call returns `400` (not processed
yet) until `avatarUrl` is set.

```ts SDK theme={null}
async function confirmAvatar(maxAttempts = 10) {
  for (let i = 0; i < maxAttempts; i++) {
    try {
      const profile = await client.updateUser({ confirmAvatar: true });
      if (profile.avatarUrl) {
        return profile.avatarUrl;
      }
    } catch (err: any) {
      if (err.status !== 400) throw err;
    }

    await new Promise((r) => setTimeout(r, 1000));
  }

  throw new Error("Avatar processing timed out");
}
```

```bash cURL theme={null}
curl -X PATCH https://api.agg.market/users/me \
  -H "x-app-id: $AGG_APP_ID" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "content-type: application/json" \
  -d '{ "confirmAvatar": true }'
```

[Update username](https://docs.agg.market/api-reference/users/update-username.md). The same call sets `username` and
`removeAvatar`.

If you prefer not to confirm explicitly, poll [Get current user](https://docs.agg.market/api-reference/users/get-current-user.md)
until `avatarUrl` appears:

```ts theme={null}
async function waitForAvatar(maxAttempts = 10) {
  for (let i = 0; i < maxAttempts; i++) {
    const profile = await client.getCurrentUser();
    if (profile.avatarUrl) {
      return profile.avatarUrl;
    }
    await new Promise((r) => setTimeout(r, 1000));
  }
  throw new Error("Avatar processing timed out");
}

const avatarUrl = await waitForAvatar();
```

## Constraints

| Constraint | Value |
| - | - |
| Max file size | 5 MB |
| Allowed types | JPEG, PNG, GIF, WebP |
| Processing time | Usually 1-3 seconds |
| CDN variants | 44px, 48px, 60px, original, raw |

## Removing an Avatar

Same [Update username](https://docs.agg.market/api-reference/users/update-username.md) call, with `removeAvatar: true`.

```ts SDK theme={null}
await client.updateUser({ removeAvatar: true });
```

```bash cURL theme={null}
curl -X PATCH https://api.agg.market/users/me \
  -H "x-app-id: $AGG_APP_ID" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "content-type: application/json" \
  -d '{ "removeAvatar": true }'
```

## Error Handling

```ts theme={null}
try {
  const { uploadUrl } = await client.createAvatarUploadUrl(file.type);

  const uploadRes = await fetch(uploadUrl, {
    method: "PUT",
    headers: { "Content-Type": file.type },
    body: file,
  });

  if (!uploadRes.ok) {
    throw new Error(`Upload failed: ${uploadRes.status}`);
  }

  const avatarUrl = await confirmAvatar();
  console.log("Avatar ready:", avatarUrl);
} catch (err: any) {
  if (err.status === 415) {
    console.error("Unsupported file type");
  } else {
    console.error("Avatar upload failed:", err);
  }
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.