An instance is one stage of the Alchemy Stack ScwrapInstance in apps/infra/alchemy.instance.ts: two Workers, a proxy relay Container and two R2 buckets in your Cloudflare account, at a hostname of your own. Each stage has its own state, resources and credentials.
Setting up a new instance
You need a Cloudflare account on the Workers Paid plan ($5 a month, Monthly cost) with R2 enabled, a domain in that account, and Node.js and pnpm (Requirements). The whole setup takes about five minutes.
Get the repository.
shgit clone https://github.com/J4K4-Dev/scwrap.git && cd scwrap && pnpm installRun the setup with a stage name of your choice, such as
acme. The stage names the Workers,scwrap-instance-<stage>.shpnpm infra:instance:setup --stage acmeIt asks for three values and writes them, with everything it derives or generates, to
.env.acmein the repository root, which Git ignores:It asks for What to enter Instance URL An HTTPS origin on a domain in the account, such as https://scwrap.example.com. The hostname must not be in use yet.Infrastructure token Open Create the infrastructure token, choose the account, keep every permission it selects, create the token and paste it. Deployment token Open Create the deployment token, the same way. The instance keeps it to install Releases. It finds the account ID and the zone of your hostname with the infrastructure token, and generates the run token and the deployer token. Run it again at any time: it keeps what the file holds and asks only for what is missing.
Deploy the instance.
shpnpm infra:instance:deploy --stage acmeAlchemy shows the plan, every resource as
create, and asksDeploy?before it applies it; it takes about a minute and a half. When the account already holds other Workers or Secrets Store secrets, the command stops and lists them; put the instance in an account of its own, or addSCWRAP_INSTANCE_SHARED_ACCOUNT=acceptto.env.acme(A dedicated account).Deploy the example Scraper and run it. The
scwrapCLI reads the instance URL and deployer token from the environment, so load the file first:shset -a; . ./.env.acme; set +a pnpm turbo run build --filter='@scwrap/infra^...' pnpm scwrap deploy earthquakes curl -sS -H "authorization: Bearer $SCWRAP_INSTANCE_RUN_TOKEN" -H 'content-type: application/json' \ -d '{"minMagnitude":"2.5","period":"day"}' "$SCWRAP_INSTANCE_URL/scrapers/earthquakes/run"A new Release takes a few seconds to reach the edge; until then the run answers
unknown_scraper, so try again. The answer holds aresultId. Read the Run withGET /results/<resultId>and its items withGET /results/<resultId>/items(HTTP API), or connect an MCP client to/mcp(MCP).
Give callers the run token, SCWRAP_INSTANCE_RUN_TOKEN, and your Scraper repository's deploys the deployer token, SCWRAP_INSTANCE_DEPLOYER_TOKEN (Tokens). Deploy your own Scrapers from their repository (Installation). Running pnpm infra:instance:deploy --stage acme again upgrades the platform and keeps every installed Release.
Keep .env.acme safe: it holds every credential of the instance. To keep them in a secret manager instead, store the same names there and start the commands with them in the environment, such as op run -- pnpm infra:instance:deploy --stage acme; values in the environment take precedence over the file.
Removing an instance
pnpm infra:instance:destroy --stage acmeIt asks you to type the stage name, then deletes the stage's Workers with their Durable Objects, its Custom Domain, Container applications, Secrets Store secrets, buckets with every Run and item in them, AI Gateway and Alchemy state, and checks that none of them is left. It touches nothing named for another stage. .env.acme stays; delete it and the two Cloudflare tokens yourself.
Variables
pnpm infra:instance:* reads a stage's variables from the process environment and from .env.<stage> in the repository root; the environment wins. Every name is declared in .env.schema.
| Variable | Required | Is |
|---|---|---|
SCWRAP_INSTANCE_URL | Yes | The instance's HTTPS origin (Rules for the URL). |
CLOUDFLARE_API_TOKEN | Yes | The infrastructure token Alchemy uses: Account Settings Read, Secrets Store Read and Write, Workers Containers Write, Workers R2 Storage Write, Workers Scripts Write, AI Gateway Write, Zone Read. |
SCWRAP_INSTANCE_DEPLOY_TOKEN | Yes | The deployment token, Workers Scripts Write and Secrets Store Write. Becomes the controller Worker's DEPLOY_API_TOKEN. |
SCWRAP_INSTANCE_RUN_TOKEN | Generated | The run token, a random value of at least 32 characters. Its digest becomes RUN_TOKEN_SHA256. |
SCWRAP_INSTANCE_DEPLOYER_TOKEN | Generated | The deployer token, the scwrap CLI's bearer, a random value. Its digest becomes DEPLOYER_TOKEN_SHA256. |
CLOUDFLARE_ACCOUNT_ID | Derived | The account the instance lives in. When unset, the only account the infrastructure token reaches; a token that reaches several needs it set. |
SCWRAP_INSTANCE_ZONE_ID | Derived | The zone of the hostname. When unset, the zone of the account whose name the hostname ends in, found with Zone Read. |
SCWRAP_INSTANCE_SHARED_ACCOUNT | No | accept to run in an account that holds other Workers or secrets (A dedicated account). |
SCWRAP_INSTANCE_RUN_TOKENS, SCWRAP_INSTANCE_DEPLOYER_TOKENS | No | Named tokens, each optionally limited to some Scrapers. |
SCWRAP_INSTANCE_GRANTS | No | The grants table, not secret. Becomes GRANTS. |
SCWRAP_INSTANCE_REDIRECT_ORIGIN | No | An HTTPS origin that every GET or HEAD outside the API answers 301 to, with the same path and query (Other paths); unset, those answer 404. Becomes REDIRECT_ORIGIN. |
SCWRAP_INSTANCE_DASHBOARD | No | on to serve the dashboard at /dashboard/ from its own Worker, scwrap-instance-<stage>-dashboard; off or unset serves none. |
SCWRAP_INSTANCE_BROWSER_ORIGINS | No | A JSON array of HTTPS origins whose browser pages may read /scrapers and /results (Browsers); unset, none may. Becomes BROWSER_ORIGINS. |
SCWRAP_INSTANCE_RESULT_RETENTION_DAYS | No | How many days Run records and items are kept, a whole number from 1 to 3650; 30 when unset. Sets the RunResults lifecycle rule and becomes RESULT_RETENTION_DAYS. |
SCWRAP_INSTANCE_RELEASE_ENV | No | The JSON object that becomes RELEASE_ENV on a fresh install: the Apify Proxy and Workers AI settings the front desk reads. An AI token inside it needs Workers AI Read and AI Gateway Run. |
SCWRAP_INSTANCE_SCRAPER_SETTINGS | No | The JSON object from Scraper IDs to their settings that becomes SCRAPER_SETTINGS on a fresh install; {} when unset. |
BROWSER_USE_API_KEY | No | The Browser Use key. Becomes BROWSER_USE_API_KEY on a fresh install. |
SCWRAP_INSTANCE_TRACES_URL, …_HEADERS, …_TOKEN, …_PROTOCOL, …_LINK | No | Connect a traces provider with an ingest-only credential. Without SCWRAP_INSTANCE_TRACES_URL the instance exports no traces (Connecting your provider). |
The wrapper, with-credentials.ts --instance, names every missing required variable without printing any value, derives the two IDs, and starts the command with the variables in its environment. Credentials never reach a log or a shell argument.
Commands
pnpm infra:instance:setup --stage <stage>
CI=1 pnpm infra:instance:plan --stage <stage> --no-input --detailed
pnpm infra:instance:deploy --stage <stage> --detailed
pnpm infra:instance:destroy --stage <stage>Plan and deploy build the platform first. Planning is read-only. When you deploy by hand, never pass --yes, never approve the creation or upgrade of Alchemy's state store as a side effect, and never adopt existing resources. A stage selects state, not credentials: check that the account, zone, stage and hostname are the ones you mean before you deploy.
In CI
To deploy an instance from GitHub Actions, add the three required values and the two generated tokens as secrets of a GitHub environment and pass them to the deploy step:
- name: Deploy the instance
env:
CI: "1"
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
SCWRAP_INSTANCE_URL: ${{ secrets.SCWRAP_INSTANCE_URL }}
SCWRAP_INSTANCE_DEPLOY_TOKEN: ${{ secrets.SCWRAP_INSTANCE_DEPLOY_TOKEN }}
SCWRAP_INSTANCE_RUN_TOKEN: ${{ secrets.SCWRAP_INSTANCE_RUN_TOKEN }}
SCWRAP_INSTANCE_DEPLOYER_TOKEN: ${{ secrets.SCWRAP_INSTANCE_DEPLOYER_TOKEN }}
run: pnpm infra:instance:deploy --stage <stage> --no-input --yes < /dev/nullThis repository's own deploy job in ci.yml deploys production from OpenBao instead (below), and runs only in this repository: its if: guard names this repository's github.repository_id. In a copy, put your repository's ID there and replace its two OpenBao steps with the env block above.
Using OpenBao
The maintainer's production instance, stage production at https://scwrap.j4k4.dev, keeps its variables in an OpenBao document instead of a file. The wrapper reads a stage's document when an OpenBao address is set and the stage has no .env.<stage> file, or when SCWRAP_CREDENTIAL_SOURCE=openbao; SCWRAP_CREDENTIAL_SOURCE=env turns it off.
| Setting | Default | Is |
|---|---|---|
SCWRAP_OPENBAO_ADDR | PERSONAL_OPENBAO_ADDR | The server's HTTPS address. |
SCWRAP_OPENBAO_MOUNT | scwrap | The KV v2 mount that holds <stage>/instance. |
SCWRAP_OPENBAO_LOGIN | amp | amp exchanges a short-lived Amp orb identity token for a role through the amp auth method and revokes the session when the command ends. token uses an existing token. |
SCWRAP_OPENBAO_ROLE | personal-manager | The role of the amp login. |
SCWRAP_OPENBAO_AUDIENCE | urn:openbao:amp:personal:manager | The audience of the Amp identity token. |
SCWRAP_OPENBAO_TOKEN | The token of the token login. |
The document's fields replace any SCWRAP_INSTANCE_* and BROWSER_USE_API_KEY values already in the environment, except SCWRAP_INSTANCE_SHARED_ACCOUNT, which production plans set there, and a missing document stops the command. Production's document is scwrap/production/instance. CI's deploy job reads it through the GitHub OIDC role scwrap-ci and revokes its token before deploying; it reads only the fields it lists, so add a new variable to that list once the document holds it.
What the Stack creates
Before it creates or changes anything, the Stack builds the platform and, when the stage's controller Worker exists, asks it to check the platform against the installed Releases (Compatibility check). A platform that would break one stops plan and apply with the Releases named. A new stage skips the check, so nothing looks up its hostname before the Custom Domain exists and resolvers do not cache it as missing.
The Stack creates resources in this order.
- The R2 buckets. Both private, with no public domain, and retained when the Stack is destroyed.
Resultsholds Release artifacts and platform manifests, with no lifecycle rule; its name predates the split.RunResultsholds Run records and items, with one lifecycle rule,delete-expired-results, that deletes every objectSCWRAP_INSTANCE_RESULT_RETENTION_DAYSdays after it was written (30 by default). - The release Worker's bootstrap. The
ReleaseBootstrapresource callsbootstrapInstance, which uploads the release Worker only when it does not exist yet: the platform with no Releases, the initial migration for theRunnerclass and the bootstrap secrets. On an installed instance it changes nothing. The controller binds the release Worker, and Cloudflare refuses a service binding to a Worker that does not exist (error 10143), so this comes first. - The build Worker and its toolchain.
Toolchains, a private R2 bucket.Toolchainpacks the seven SDKs from the repository and installs them with effect, TypeScript,@effect/tsgo,@effect/platform-node, esbuild and the curated packages Scrapers may import (cheerio) at their pinned versions into one archive,toolchains/<digest>.tgz(Builds). It builds and uploads only when no archive with that digest exists, and deletes the previous one after an upgrade; building needs Linux x64, as CI has, because the Effect patch rewrites the native TypeScript binary.Builderisscwrap-instance-<stage>-builder, an Alchemy Worker with no secrets, no route and no workers.dev URL, itsBuildCheckerDurable Object class and theirBuildBoxContainer application. It takes its name fromALCHEMY_STAGE, whichpnpm infra:instance:*and CI set to the stage; the Stack stops when the two differ. The controller binds it, so it comes before the controller. - The dashboard Worker. Only while
SCWRAP_INSTANCE_DASHBOARDison:scwrap-instance-<stage>-dashboard, an Alchemy Worker with no bindings, no route and no workers.dev URL that serves the dashboard. It comes before the controller, which binds it. - The controller Worker. The
Controllerresource uploadsscwrap-instance-<stage>-controllerwith the Cloudflare API,DEPLOY_API_TOKENincluded, and attaches the instance hostname to it as its Custom Domain. It refuses a hostname that another Worker serves, then waits for the controller to answerGET /deployments. workers.dev and preview routes are disabled. Both are retained on teardown. - The release Worker's platform. The
SharedWorkerresource builds the platform and callsinstallInstance, which uploads it through the controller. It has no route of its own: the controller forwards every request that is not a deployment request to it through a service binding.installInstanceturns off its workers.dev and preview URLs on every apply. - The AI Gateway.
scwrap-captcha-<stage>, authenticated, with logs and no cache, which Clef captcha calls from Worker releases with theai:workers-aigrant go through with the AI token (Workers AI). - The Container application.
ProxyRelay, created withscheduling_policy: "durable_object"and attached to the Worker'sProxyRelayclass, the proxy relay.
Runner, the class of Run owners and idempotency keepers, and ProxyRelay are Durable Object classes of the release Worker. The deployment controller is a Durable Object class of the controller Worker, which release code cannot reach: it shares no isolate with the release Worker, and the release Worker has no binding to it.
One owner of Worker uploads
Alchemy never uploads the release Worker on its own. Installing, upgrading and repairing all go through installInstance and the instance's deployment service, so a platform upgrade keeps the installed Releases. The controller Worker is small and holds no Releases, so the Stack uploads it directly.
The Stack's state holds identifiers and digests. It holds no module bodies and no secret values. An unchanged platform is a no-op, and deploying a Scraper does not change the platform digest.
Things that cannot be changed in place
A Worker's name, hostname, zone and bucket cannot be moved to another instance. Use a new stage for a separate instance. A change to the Container application's account, Worker or class replaces it.
Bindings
The release Worker runs with the nodejs_compat compatibility flag and is configured through these bindings.
| Binding | Value |
|---|---|
RUNNER | The Runner Durable Object class: each Run owner, run-<resultId>, and each idempotency keeper, idempotency-<hash>. It has no Container. |
PROXY_RELAY | The ProxyRelay Durable Object class, attached to its own Container application, the proxy relay. Only the front desk uses it. |
GRANTS | The owner's grants table, plain text, from SCWRAP_INSTANCE_GRANTS. |
RESULTS | The RunResults bucket: Run records, items and the run index. |
ARTIFACTS | The Results bucket, read for Release modules and scwrap-sdk.mjs. |
INSTANCE_URL | The instance's public HTTPS origin. |
BROWSER_ORIGINS | Only while SCWRAP_INSTANCE_BROWSER_ORIGINS is set: the origins allowed to read the API from a browser, as JSON. |
REDIRECT_ORIGIN | Only while SCWRAP_INSTANCE_REDIRECT_ORIGIN is set: the origin that other GET requests are redirected to. |
RUN_TOKEN_SHA256 | The SHA-256 digest of the run token (Tokens). Plain text: the Worker stores no copy of the token. |
RESULT_RETENTION_DAYS | The result retention in days, plain text, from SCWRAP_INSTANCE_RESULT_RETENTION_DAYS. The MCP list_runs tool states it. |
RUN_TOKENS | The named run tokens as a JSON array of name, digest and optional Scraper IDs (Named tokens). |
LOADER | The Worker Loader that runs each isolated Release in its own Dynamic Worker. |
BROWSER | The account's Browser Run, which browserRun launches Chromium through in Worker releases (Providing a Browser). |
RELEASE_ENV | Optional secret. A JSON object of strings that only the front desk reads: the Apify Proxy settings, CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_AI_TOKEN for Workers AI. |
SCRAPER_SETTINGS | Optional secret. A JSON object from Scraper IDs to objects of strings, the only Config a Worker release reads: each Run gets its own Scraper's entry plus its own SCWRAP_RUN_ID, its result ID (Settings). A value that is not such an object fails every Worker Run. While it is unset, every Run gets RELEASE_ENV without the keys the front desk holds. |
BROWSER_USE_API_KEY | Optional secret. The Browser Use key the front desk opens each Browser Use session with, for Worker releases that use browserUse. |
TRACES_URL, TRACES_PROTOCOL, TRACES_LINK | Only while the instance exports traces: the OTLP/HTTP traces endpoint, http/protobuf when set, and the URL template of a trace (Tracing). |
TRACES_HEADERS, TRACES_TOKEN | Optional. Secrets Store secrets scwrap-instance-<stage>-traces-headers and -traces-token: the headers sent with every export and the bearer token. Unlike the secrets above, applying the Stack updates them. |
The release Worker also exports an inert DeploymentController class. Production created it when the controller still lived there, and only a migration could remove it.
The controller Worker is configured through these bindings.
| Binding | Value |
|---|---|
DEPLOYMENTS | The DeploymentController Durable Object class. Always addressed as instance. |
DeploymentArtifacts | The Results bucket, the release Worker's ARTIFACTS. |
RELEASE | A service binding to the release Worker, for every request that is not a deployment request. |
DASHBOARD | Only while SCWRAP_INSTANCE_DASHBOARD is on: a service binding to the dashboard Worker, which the controller forwards /dashboard/* to. |
DEPLOY_ACCOUNT_ID, DEPLOY_WORKER_NAME | The instance's account and the release Worker's name. |
DEPLOYER_TOKEN_SHA256 | The SHA-256 digest of the deployer token. |
DEPLOYER_TOKENS | The named deployer tokens, the same shape as RUN_TOKENS. |
DEPLOY_API_TOKEN | Secret. The instance's own Cloudflare token with Workers Scripts Write and Secrets Store Edit (why it cannot be narrower). The Stack uploads it with every controller upload, so applying the Stack rotates it. |
Both Workers use compatibility date 2026-11-12, without compatibility flags.
The release Worker's secrets are installed when it is first created, and later uploads keep them, so applying the Stack does not change them. Rotate them with scwrap secrets set. The run and deployer tokens are not secrets of either Worker, so applying the Stack rotates them.
Tokens
Callers and deployers hold different bearer tokens. Each route group checks only its own, and either group can hold several named tokens.
| Token | Variable | Accepted on | Held by |
|---|---|---|---|
| Run token | SCWRAP_INSTANCE_RUN_TOKEN | /scrapers, /results, /mcp | @scwrap/client callers, MCP clients, raw HTTP, the dashboard |
| Deployer token | SCWRAP_INSTANCE_DEPLOYER_TOKEN | /deployments, /platform-deployments | CI, the scwrap CLI and the Stack's installer |
Each token on the other group's routes answers 401, and so does any other bearer. The dashboard page at /dashboard/ takes no token: it is static and holds no data. The release Worker checks the run routes and the controller Worker the deployment routes; each compares the SHA-256 digest of the bearer with the digest it binds, so a leaked digest does not authenticate.
To rotate the run or deployer token, store a new random value under its variable and apply the Stack. The platform upgrade binds the new digest and the old value stops working.
Named tokens
Besides the one run token and the one deployer token, which are named default and reach everything, an instance can hold any number of named tokens, each optionally limited to some Scrapers. Give each caller or team its own, so a leak is contained to that token and its Scrapers, and every Run names the token that started it.
SCWRAP_INSTANCE_RUN_TOKENS='{"team-a": {"token": "<random>", "scrapers": ["fff-schedule"]}, "dashboard": {"token": "<random>"}}'
SCWRAP_INSTANCE_DEPLOYER_TOKENS='{"team-a-ci": {"token": "<random>", "scrapers": ["fff-schedule"]}}'A name is lowercase letters, digits and hyphens, and not default. A token is at least 32 characters, such as openssl rand -hex 32, and no two names share one. The Stack refuses anything else and names only the variable, never a value. It binds only the digests, as RUN_TOKENS on the release Worker and DEPLOYER_TOKENS on the controller.
| A token limited to some Scrapers | Gets |
|---|---|
| Run token | Only those Scrapers in GET /scrapers and MCP list_scrapers. Every other Scraper's run, runs list, results and items answer 404, as if it were not installed, on HTTP and MCP alike. |
| Deployer token | GET /deployments and deployment status. Deploying or removing only those Scrapers; any other command, a platform upgrade and PUT or DELETE /secrets answer 403. |
A token without scrapers reaches every Scraper, and only an unlimited deployer token can upgrade the platform or rotate secrets. Each result record carries tokenName, and every request's span carries scwrap.token.name (Tracing).
To revoke one token, delete its entry and apply the Stack. The others keep working.
A dedicated account
Put each instance in a Cloudflare account of its own. The controller's DEPLOY_API_TOKEN can replace any Worker in its account and bind any Secrets Store secret, and a platform upload chooses the release Worker's bindings, so a holder of the deployer token can reach every other Worker and secret in that account.
Cloudflare cannot narrow this token today:
- Workers Scripts Write and Secrets Store Edit are account-wide permissions (API token permissions). Edit is required to upload a Worker that binds a Secrets Store secret (Secrets Store access control).
- Cloudflare documents an "Individual Workers Editor" token scoped to one Worker (Roles and permissions). Tested on 2026-11-28, such a token could still upload to and delete other Workers in the account, create new ones and bind Secrets Store secrets through the Workers Scripts API, so it does not contain the controller.
The Stack checks the account before it plans or deploys. When the account holds Workers or Secrets Store secrets that are not the stage's own or the Alchemy state store's, it stops and lists them. Set SCWRAP_INSTANCE_SHARED_ACCOUNT=accept to run in a shared account anyway; it then logs the same list as a warning on every plan and deploy. Production runs in a shared account with that setting.
Monthly cost
An instance runs on the account's Workers Paid plan, $5 a month, and most of a small instance's usage fits in what the plan includes. These are Cloudflare's list prices as of 2026-11-30; check the linked pages before you rely on them.
| Part | Included each month | Beyond that | What an instance uses |
|---|---|---|---|
| Workers | 10 million requests, 30 million CPU ms | $0.30 per million requests, $0.02 per million CPU ms | Every API request, including each read while a caller polls a Run. |
| Durable Objects | 1 million requests, 400,000 GB-s | $0.15 per million requests, $12.50 per million GB-s | Each Run owner is billed at 128 MB for as long as its Run runs: 15 GB-s for a 2-minute Run, 75 GB-s for 10 minutes. The included duration covers about 890 hours of Runs. |
Dynamic Workers (LOADER) | 1,000 unique Dynamic Workers | $0.002 per Dynamic Worker per day | Cloudflare counts each unique Worker ID and code once per day. The instance loads each Release under a stable ID, its Scraper ID with the digests of its code, the sandbox and the shared SDK, so a Release counts once on each day it runs, however many Runs it has: 20 Releases that run daily are about 600 a month. A deploy that changes a Release's code, or every Release's after an instance update, counts it again that day. Their requests and CPU time count as Workers usage. |
| R2 | 10 GB-month, 1 million Class A and 10 million Class B operations | $0.015 per GB-month, $4.50 per million Class A, $0.36 per million Class B | Run records and items, kept for the result retention (30 days by default), plus Release artifacts and the build toolchain, about 85 MB. Each stored chunk of items is two Class A writes. |
Containers (ProxyRelay and BuildBox, both basic) | 25 GiB-hours memory, 375 vCPU-minutes, 200 GB-hours disk | $0.009 per GiB-hour, $0.072 per vCPU-hour, $0.000252 per GB-hour | Each only while it is awake: the relay from a proxied request, the build Container from a build, each until 10 minutes after its last one. Awake, a basic instance bills 1 GiB of memory and 4 GB of disk per hour, and CPU only while it works. |
For example, 20 Releases that run every day, 3,000 Runs a month of 2 minutes each, with the relay awake 60 hours:
| Item | Cost |
|---|---|
| Workers Paid plan | $5.00 |
| Workers, Durable Objects (45,000 GB-s), R2 | $0, within the included usage |
| Dynamic Workers: 20 Releases × 30 days = 600, within the 1,000 included | $0 |
| Container: 60 − 25 GiB-hours of memory, 240 − 200 GB-hours of disk | about $0.33 |
| Total | about $5.33 |
A relay kept awake all month by steady proxied traffic adds about $7 for memory and disk. The vendors a Release uses bill separately: Browser Run and Workers AI on the Cloudflare bill, and Browser Use, Apify Proxy and Axiom on their own terms.
Testing a fresh install
pnpm infra:instance:fresh --stage fresh-<name> runs the whole setup against a stage that must not exist yet, then removes it:
It runs
infra:instance:setupwithout prompts, which generates the run and deployer tokens, adds named tokens limited to Scrapers, grants the probe Scrapers below their capabilities, and applies the Stack.It deploys the earthquakes example as a Worker release, runs it once and reads back its stored items.
It checks the named tokens: a token limited to another Scraper sees no earthquakes Run and may not rotate a secret or remove the Release, and revoking one token leaves the others working.
It deploys the Capability probes from
fresh-instance/catalog.ts, one Worker release per documented Capability, and runs each whose credential it has:Probe Checks Runs when set fresh-egressOne Run fetches an IP echo directly and through apifyProxy; the two addresses must differ.APIFY_PROXY_PASSWORDfresh-browser-runbrowserRunopenshttps://scwrap.dev/and reads the title.Always fresh-browser-usebrowserUsedoes the same, in one Browser Use session with a 2-minute timeout.BROWSER_USE_API_KEYfresh-captchacaptcha-templates, firstConfidentand Clef throughAiGatewayread a fixture captcha.CLOUDFLARE_AI_TOKENfresh-throughputThree Runs: SCWRAP_FRESH_ITEMSitems without requests (20,000),SCWRAP_FRESH_FETCHESitems with one direct request each (2,000), andSCWRAP_FRESH_PROXIEDitems with one Apify Proxy request each (50, whenAPIFY_PROXY_PASSWORDis set). Each must store every item.Always SCWRAP_PROXY_GROUPSchooses the Apify Proxy group, as on any instance. The three credentials go into the stage'sRELEASE_ENV, andfresh-captcha's gateway into its entry ofSCRAPER_SETTINGS; none is required.It removes the stage with
infra:instance:destroy, which fails if anything of the stage is left.
It takes the three required variables from the environment, and it refuses a stage name that does not start with fresh-. One run takes 8 to 13 minutes. The Fresh instance workflow runs it on demand and for every v* tag, with the variables as secrets of the GitHub environment fresh-instance. Set SCWRAP_FRESH_KEEP=1 to keep the stage for inspection; remove it with infra:instance:destroy afterwards. Each run creates the instance's hostname anew; running stages back to back on one hostname can meet resolvers that still cache it as missing, for up to the zone's 30-minute negative TTL. The workflow therefore resolves through Cloudflare's 1.1.1.1, which answered for a recreated hostname within seconds where other resolvers still cached it as missing; elsewhere, point the machine at it too or wait out the TTL.
Rules for the URL
The instance URL must be an HTTPS origin without credentials, query, fragment or a path. workers.dev URLs are refused. So are cloud-backed alchemy dev and resource adoption.
Rotating secrets
| Secret | Rotate it with |
|---|---|
BROWSER_USE_API_KEY, RELEASE_ENV, SCRAPER_SETTINGS | scwrap secrets set <name>, below. |
Run token, deployer token, DEPLOY_API_TOKEN | A new value under its variable, then applying the Stack. |
TRACES_HEADERS, TRACES_TOKEN | A new value under SCWRAP_INSTANCE_TRACES_HEADERS or SCWRAP_INSTANCE_TRACES_TOKEN, then applying the Stack, which updates the Secrets Store secret. |
scwrap secrets set takes the new value on standard input and the instance and deployer token like every scwrap command:
pnpm scwrap secrets set RELEASE_ENV < release-env.jsonThe controller sets the secret under its deployment lock: it uploads a new version of the release Worker with the active platform and Releases, the new value and every other secret inherited by name, activates it like a deploy, and records that version as its own. Cloudflare's secrets endpoints take no code update strategy, so the controller does not use them. It refuses while a deployment is pending, and refuses when the active version is not the one it last deployed. It keeps no copy of the value. Store the new value under its variable too, so a fresh install of the same stage gets it.
A rotation lets active Runs finish, as any deploy does (Runs during a deploy): they keep the value they started with, and new Runs get the new one.
Other changes to the release Worker's secrets, such as Cloudflare's own secret command, create a version the controller did not make. Its outside-change guard then refuses the next deployment. Never clear pending state or weaken the guard to accept an unknown version.
The Container application
The proxy relay's Container application is declared by a small native-API provider in instance-container.ts, because the pinned Alchemy Container provider can only declare a deprecated scheduling policy.
- The provider verifies the application's name, policy and namespace before it accepts an existing application or deletes a recorded one.
- A lost create response is recovered by the stable generated name, without a second create.
- The image, size, network access and entrypoint are chosen by the
ProxyRelayDurable Object at start, not by the application declaration. - Applications with the current policy do not support
max_instances. The application runs one Container perProxyRelayDurable Object, and the Worker names one relay per platform version.
The docs site
The docs at https://scwrap.dev are the project's own site, not part of any instance: an instance, production included, serves no docs. A separate Alchemy Stack, ScwrapDocs in apps/infra/alchemy.docs.ts, uploads the VitePress build in apps/docs/.vitepress/dist as the static assets of the Worker scwrap-docs, with no script and no workers.dev URL, and attaches scwrap.dev to it as its Custom Domain. It has one stage, production, in the maintainer's account; CI's deploy job applies it before the production instance with the same Cloudflare credentials, and by hand:
pnpm infra:docs:plan --stage production
pnpm infra:docs:deploy --stage productionBoth build the docs first. Every page loads the Amp feedback widget of the j4k4dev/scwrap Amp project, allowed on https://scwrap.dev; it stays hidden until a page is opened with ?amp, and only members of the project can send comments. The production instance sets SCWRAP_INSTANCE_REDIRECT_ORIGIN=https://scwrap.dev, so a docs link to https://scwrap.j4k4.dev/<path> lands on the same page of the site. A copy of the repository that deploys its own instance needs neither this Stack nor a docs build.
Source
- alchemy.instance.ts: the Stack
- alchemy.docs.ts: the docs site
- instance-worker.ts, instance-controller.ts, instance-container.ts, instance-secret.ts: its resources
- instance-metadata.ts: the Worker's nonsecret metadata