Skip to main content
A Worker already carries everything a render needs: fetch, URL, and a place to keep a secret. This page builds one with two routes, the first registering a Photoshop template and the second rendering artwork onto it.

Build a Cloudflare Worker that renders SudoMock mockups.

Open in Cursor

Prerequisites

  • An API key from API keys. Keys begin with sm_ and travel in the x-api-key header, as Authentication describes.
  • A PSD reachable over HTTPS. Preparing a PSD covers what the file needs before it is uploaded.
  • A Cloudflare account, and Node on your machine. Wrangler arrives with the project you create in the first step.

Guide

1

Install

Create the project with C3, Cloudflare’s project generator.
Choose the Hello World template. The code below is JavaScript and lives in src/index.js.
2

Store the API key

A secret is set once and stays with the deployed Worker, so it never reaches your repository.
For wrangler dev, put the same name in a .dev.vars file and add that file to .gitignore.
.dev.vars
The two UUIDs a render addresses are not secret, so they belong in the configuration next to everything else.
wrangler.toml
The key spends credits. Keep it in a secret, never in [vars] and never in a file you commit.
3

Render from the Worker

One helper sets the header and raises anything that is not a success. The two routes then read as the two calls they are.
src/index.js
Run it locally, POST a PSD URL to /templates, and move the two UUIDs it answers with into wrangler.toml. From then on /renders takes an artwork URL and nothing else.
4

Read what comes back

A render answers with the finished image at data.print_files[0].export_path, which the Worker above hands back under a single key.
200
A render failure carries error_code, a readable message and a details.suggestion. The helper passes that body through with the status it arrived on, so the caller sees the same reason the API gave. Branch on error_code and keep a default case, so an unfamiliar code degrades into a readable failure rather than a crash. Errors lists the codes and says which statuses are worth retrying.Waiting on the API costs the Worker network time rather than compute, so a long render does not press against a CPU ceiling. What it does hold is the caller’s connection. When that is the wrong trade, send is_async as true. The call answers 202 with a job_id straight away, which you either poll at GET /api/v1/jobs/{job_id} or let a webhook hand to a second route on the same Worker.Each API call is one subrequest, and Workers caps subrequests per invocation. A queue of renders belongs behind is_async rather than a loop inside one request.

Next steps

Webhooks

Let a queued render call a Worker route instead of being polled.

Errors

Every error code, and which statuses are worth retrying.

Fit and blend modes

What fit and blending_mode do to the finished image.