Public
Glowcast — AI photo restyling app with Replicate PuLID
aiimage-generationreactreplicate
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.

Glowcast

Restyle any photo with AI. React frontend + Hono API in a single val, backed by ByteDance PuLID on Replicate.

Live: https://glowcast.val.run — open index.ts

⚠️ Two setup steps before it works

1. Add the Replicate API key to this val

Environment variables do not transfer when a val is created, so this val does not have the key yet. The health check currently reports "hasReplicateKey": false.

👉 Add REPLICATE_API_KEY here: https://www.val.town/x/manassehRight/glowcast/environment-variables?key=REPLICATE_API_KEY

2. Add credit to your Replicate account

PuLID is a paid model and the account has no credit, so every generation returns 402 Insufficient credit. Add credit at https://replicate.com/account/billing#billing, then wait a few minutes.

Without a payment method on file, Replicate also throttles prediction creation to 6/min with a burst of 1, so two people generating simultaneously will make one of them get a 429. Adding the payment method lifts that too.

Confirm both are sorted without spending credit:

curl https://glowcast.val.run/api/health # {"ok":true,"model":"bytedance/pulid","hasReplicateKey":true}

Architecture

Rendering mermaid diagram...
index.ts                      ← Hono: routes + HTML shell
replicate.ts                  ← server-side PuLID call (testable in isolation)
frontend/
  root.tsx                    ← HTML shell, stamps immutable asset URLs
  index.tsx                   ← React entrypoint
  favicon.svg
  lib/resize.ts               ← browser: downscale + download helpers
  components/
    App.tsx                   ← screen layout and state
    StylePicker.tsx           ← preset styles (label → prompt)

Why the UI and API live in one val: they are same-origin, so the browser calls /api/restyle with a relative path. No CORS, no absolute URL that can be misconfigured, no third-party host in the middle. This was the single biggest source of bugs in the previous Netlify setup.

API

GET /api/health

{ "ok": true, "model": "bytedance/pulid", "hasReplicateKey": true }

POST /api/restyle

{ "imageBase64": "<data URI, raw base64, or https URL>", "prompt": "..." }

Returns { "output": ["https://replicate.delivery/..."], "predictionId": "..." }.

imageBase64 is normalised server-side: a bare base64 string is wrapped in a data: URI automatically, because Replicate rejects bare base64.

StatusMeaning
400Missing input, or Replicate rejected the image/prompt
402No Replicate credit
429Replicate rate limit
502Upstream/generation error — detail carries the raw message
504Exceeded the 50s generation deadline

Notable implementation details

  • Version-based endpoint. /v1/models/bytedance/pulid/predictions returns 404 The requested resource could not be found. for this account even though GET /v1/models/bytedance/pulid succeeds. replicate.ts uses /v1/predictions with a pinned version hash instead.
  • Images are downscaled in the browser to 1024px before encoding (lib/resize.ts). Phone photos base64-encoded can exceed 10MB, and an oversized body is rejected before it reaches the server — which the browser surfaces as a bare TypeError: Failed to fetch.
  • Downloads fetch into a blob first, because Replicate serves results from a different origin where the download attribute is ignored.

Not yet built

  • "Free generations left today" counter. The old app showed one, but it was cosmetic. A real per-day limit needs std/sqlite plus some notion of user identity — worth adding if you want to cap usage.
  • Shared types between the API and the UI. replicate.ts exports RestyleOutcome; wiring it through to the client would catch shape drift.

Reference

Template: templates/react-hono-starter. The earlier Netlify-based backend remains at manassehRight/glowcast-backend.