Skip to main content

Profile picture API (/v1/me/avatar)

Lets a signed-in user upload, show, replace and remove their own profile picture (display picture) from the profile settings page. Built for both the Flutter app and the web console.

The image is stored in a private S3 bucket. The API never sends or receives image bytes. You upload straight to S3 with a short-lived signed URL, and you read the picture through another short-lived signed URL.

:::info Deployment status Committed 2026-11-11 to cse-http-be; not deployed yet. The routes go live on dev at the next npm run deploy:dev, which updates the cse-http-be-dev-data stack (new bucket lifecycle rule) and the cse-http-be-dev-api-account stack (four new routes and the S3 grant). No call has been captured. Every example below is an illustrative example built from the Zod schemas in packages/core/src/schemas.ts and the handler packages/api/src/handlers/me-avatar.ts. None is a real response. :::

How it works​

  1. Ask for an upload URL: POST /v1/me/avatar/uploads.
  2. Upload the file to S3: PUT the raw bytes to upload.url with upload.headers, exactly as given.
  3. Set it as your picture: PUT /v1/me/avatar with the uploadId. The server checks the file, saves it, and deletes your previous picture.
  4. Show it: use avatar.url from PUT /v1/me/avatar, GET /v1/me/avatar or GET /v1/me/settings (account.avatar).

Common to all four routes​

AspectValue
AuthCognito JWT (Authorization: Bearer <token>). Never public
AuthZSelf only. Every route acts on the caller's own account (sub from the token). There is no user ID in the path or body, so nobody can read or change another user's picture
HeadersAuthorization (required). Content-Type: application/json on bodies (else 415). X-Org-Id optional: if sent, it must be one of your ACTIVE memberships, else 403 FORBIDDEN. X-Correlation-Id optional
BodiesStrict: unknown keys → 422 VALIDATION_FAILED. Invalid JSON → 400 MALFORMED_REQUEST
Query paramsNone. Any query parameter → 400 VALIDATION_FAILED
Errors{ "error": { "code", "message", "details"?, "requestId" } }
Common errors401 gateway (bare {"message":"Unauthorized"}) · 401 SESSION_REVOKED · 403 FORBIDDEN · 500 INTERNAL · 503 DEPENDENCY_UNAVAILABLE (also when the bucket is not configured)

Accepted images: PNG, JPEG or WebP, up to 2 MiB (2,097,152 bytes). SVG is refused (it can carry script). HEIC is refused (browsers cannot show it).

POST /v1/me/avatar/uploads: get an upload URL​

Body (CreateAvatarUploadInput):

FieldTypeValidation
contentTypestringimage/png, image/jpeg or image/webp
sizeBytesinteger1 to 2,097,152. Must be the exact byte length of the file you will upload

Limit: 20 upload URLs per user per hour. Past that → 429 RATE_LIMITED with Retry-After.

Response 201 (illustrative example):

{
"uploadId": "01JCF6Z3M5W8Z0XK9R4T6V1AA0",
"upload": {
"url": "https://cse-http-be-dev-service-files.s3.us-east-1.amazonaws.com/avatar-pending/<sub>/01JCF6Z3M5W8Z0XK9R4T6V1AA0?X-Amz-Algorithm=…&X-Amz-Signature=…",
"method": "PUT",
"headers": {
"Content-Type": "image/jpeg",
"Content-Length": "48213",
"x-amz-server-side-encryption": "AES256"
},
"expiresAt": "2026-11-11T10:15:00.000Z"
}
}
  • upload.url is valid for 15 minutes.
  • Content-Type, Content-Length and x-amz-server-side-encryption are part of the signature. Send all three headers with these exact values, or S3 answers 403 SignatureDoesNotMatch. A file of a different size or type is refused by S3 itself.
  • Nothing changes on your profile yet. An upload that is never set as the picture is deleted automatically after 1 day.
StatusCodeWhen
201—upload URL created
422VALIDATION_FAILEDtype not PNG/JPEG/WebP, size out of range, unknown field
429RATE_LIMITEDmore than 20 per hour
400 / 401 / 403 / 415 / 500 / 503common—

Upload the file to S3​

Send the image bytes as the raw request body (not multipart, not base64) to upload.url, with upload.headers. S3 answers 200 with an empty body. This request goes to S3, not to the API, so do not add the Authorization header.

curl -X PUT "<upload.url>" \
-H "Content-Type: image/jpeg" \
-H "Content-Length: 48213" \
-H "x-amz-server-side-encryption: AES256" \
--data-binary @avatar.jpg

Web (the bucket's CORS allows PUT and GET from the configured webOrigins; on dev the default is http://localhost:5173):

// Browsers set Content-Length themselves from the body; it must equal sizeBytes.
const { 'Content-Length': _len, ...headers } = upload.headers;
await fetch(upload.url, { method: 'PUT', headers, body: file });

PUT /v1/me/avatar: set the uploaded image as your picture​

Body (SetAvatarInput):

FieldTypeValidation
uploadIdstringthe ULID from POST /v1/me/avatar/uploads

What the server checks before it accepts the image:

  1. The file was uploaded (else 409 INVALID_STATE, details.reason: OBJECT_MISSING).
  2. Its stored type is PNG, JPEG or WebP and its size is at most 2 MiB.
  3. Its first bytes really are that image type (magic bytes). A renamed HTML or PDF file is refused even if it was uploaded with Content-Type: image/png.

If a check in 2 or 3 fails, the upload is deleted. Start again with a new POST /v1/me/avatar/uploads.

On success the image becomes your picture, your previous picture is deleted from S3, and the profile version (the ETag of PATCH /v1/me/profile) goes up by 1.

Safe to retry: sending the same uploadId again after it succeeded returns the current picture, with no further change.

Response 200 (illustrative example):

{
"avatar": {
"avatarId": "01JCF6Z3M5W8Z0XK9R4T6V1AA0",
"contentType": "image/jpeg",
"sizeBytes": 48213,
"updatedAt": "2026-11-11T10:01:12.000Z",
"url": "https://cse-http-be-dev-service-files.s3.us-east-1.amazonaws.com/avatars/<sub>/01JCF6Z3M5W8Z0XK9R4T6V1AA0?X-Amz-Algorithm=…&X-Amz-Signature=…",
"expiresAt": "2026-11-11T10:06:12.000Z"
}
}
StatusCodeWhen
200—picture set (or already set with this uploadId)
409INVALID_STATEdetails.reason: OBJECT_MISSING: nothing uploaded for this uploadId, or the upload expired (1 day)
422VALIDATION_FAILEDuploadId is not a ULID; or details.reason is TYPE_MISMATCH, SIZE_MISMATCH or NOT_AN_IMAGE
400 / 401 / 403 / 415 / 500 / 503common—

GET /v1/me/avatar: read your picture​

Response 200: { "avatar": AvatarView } as above, or, when no picture is set:

{ "avatar": null }

The same avatar object (or null) is also returned in:

  • GET /v1/me/settings → account.avatar, so the settings page needs no extra call (Account settings);
  • PATCH /v1/me/profile → account.avatar.

It is not part of the legacy mobile GET /v1/me response. That shape is a shipped contract and stays unchanged. Use GET /v1/me/avatar (or /v1/me/settings) in the app header.

DELETE /v1/me/avatar: remove your picture​

No body. Removes the picture and deletes the image from S3. Answers 204 No Content, also when no picture was set. Show the initials (account.initials) afterwards.

Showing the picture: signed URLs and caching​

  • avatar.url is a signed S3 URL, valid for 5 minutes (expiresAt). Load it with a plain GET, with no Authorization header. S3 serves it inline with the stored Content-Type.
  • The URL is different on every read, so don't use it as a cache key. Use avatarId: it changes only when a new picture is set.
    • Flutter (cached_network_image): CachedNetworkImage(imageUrl: avatar.url, cacheKey: avatar.avatarId).
    • Web: keep the image in memory keyed by avatarId. If an <img> gets a 403 (URL expired), call GET /v1/me/avatar again for a fresh URL.
  • When avatar is null, or the image fails to load, show account.initials.
  • Don't store the URL long term (for example in local storage). Store avatarId and ask the API for a new URL.

Client checklist (Flutter and web)​

  1. Let the user pick or take a photo.
  2. Crop and re-encode it on the device to a square, at most 512×512, JPEG (quality ~85) or PNG. This keeps it well under 2 MiB and removes EXIF metadata such as GPS location (the server does not strip it, see Security). Convert HEIC (iPhone) to JPEG.
  3. POST /v1/me/avatar/uploads with the exact contentType and byte length.
  4. PUT the bytes to upload.url with upload.headers unchanged, within 15 minutes.
  5. PUT /v1/me/avatar with uploadId. On a network error, retry the same call (it is safe).
  6. Show avatar.url from the response, cached by avatarId.

Error handling:

You getDo
403 from S3 on uploadHeaders changed or the URL expired: start again at step 3
409 OBJECT_MISSINGThe S3 upload didn't finish or is older than 1 day: start again at step 3
422 NOT_AN_IMAGE / TYPE_MISMATCH / SIZE_MISMATCHThe file is not a real PNG/JPEG/WebP of that size: re-encode and start again
429 RATE_LIMITEDWait Retry-After seconds
403 from S3 on displayURL expired: call GET /v1/me/avatar

Security​

ConcernHow it is handled
Public accessThe bucket blocks all public access, requires TLS and encrypts every object (SSE-S3). Images are only reachable through signed URLs
Reading someone else's pictureKeys are built by the server from the token's sub and a server-made ID. The client never sends a key, and no route takes a user ID
Signed URL lifetimeUpload: 15 minutes. Download: 5 minutes
Wrong file type or sizeType and size are signed into the upload URL (S3 refuses others), then checked again on PUT /v1/me/avatar together with the file's magic bytes
Script in images (XSS)SVG is refused. Downloads are served with the confirmed image Content-Type
Abuse20 upload URLs per user per hour; unused uploads are deleted after 1 day; a replaced or removed picture is deleted
Lambda permissionsThe settings Lambdas can read, write and delete only avatars/* and avatar-pending/* in the bucket. No list permission, no other prefix
LogsSigned URLs are never logged
Image metadataNot stripped by the server. JPEG EXIF (GPS, camera) stays in the file unless the client re-encodes it. See KI-50

Storage​

WhatWhere
Picture referenceDynamoDB USER#<sub> / PROFILE, attribute avatar = { avatarId, contentType, sizeBytes, updatedAt } (never returned as is)
Pending uploads3://cse-http-be-<stage>-service-files/avatar-pending/<sub>/<uploadId>, deleted after 1 day by a lifecycle rule
Current pictures3://cse-http-be-<stage>-service-files/avatars/<sub>/<avatarId>

Source: packages/core/src/services/avatar.ts, packages/api/src/handlers/me-avatar.ts, lib/routes.ts (avatars), lib/route-family.ts, lib/data-stack.ts, docs/openapi.yaml.