@scwrap/platform-apify is the provider behind proxy routes. It turns a Scraper's ProxyRoute into an Apify Proxy connection.
| Export | Import from | Runs in | Provides |
|---|---|---|---|
apifyProxy | @scwrap/platform-apify | A Worker release on an instance | ProxiedHttpClient, through the instance's front desk |
apifyProxy | @scwrap/platform-apify/node | Node | ProxiedHttpClient, over Node's HTTP agent |
apifyProxyEndpoint(route) | @scwrap/platform-apify | Anywhere | The proxy server, username and password for a route, read from the settings below; the front desk uses it |
Both versions of apifyProxy cover every country and apply a route the same way.
Configuration
The Node version reads these settings through Effect's Config. On an instance the front desk reads them from RELEASE_ENV and keeps them away from Worker releases (Credentials).
| Variable | Default | Meaning |
|---|---|---|
APIFY_PROXY_PASSWORD | Required | The proxy password of the Apify account. |
SCWRAP_PROXY_GROUPS | None | Proxy groups, joined with +, for example RESIDENTIAL. |
SCWRAP_RUN_ID | None | Names the session of one Run. An instance sets it to each Run's result ID. |
ACTOR_RUN_ID | local | Names the shared session, and every session when SCWRAP_RUN_ID is unset. |
APIFY_PROXY_HOSTNAME | proxy.apify.com | The proxy host. |
APIFY_PROXY_PORT | 8000 | The proxy port. |
The password is a proxy credential, not an Apify management token. Never give a Scraper a management token.
How a route becomes a proxy
A route's country is a CountryCode, such as BR or FR; the compiler rejects anything else.
From the route and the settings, the provider builds Apify Proxy's username: the groups, the session and the country.
groups-RESIDENTIAL,session-<run id>,country-FRRequests with the same session leave from the same address. A route's session chooses which one it uses:
session | Session | Use it when |
|---|---|---|
"run", the default | SCWRAP_RUN_ID, so each Run that sets it has its own address; ACTOR_RUN_ID when it is unset | Runs should not look like one visitor, or one Run's block should not affect another. |
"shared" | ACTOR_RUN_ID, the same for every Run | Runs should keep one address, for example to reuse a site's trust in it. |
ProxiedHttpClient.route({ country: "BR", session: "shared" });It also annotates the current span, so a trace shows where a request went out:
| Span attribute | Value |
|---|---|
proxy.provider | apify-proxy |
proxy.country | The route's country, when it has one. |
proxy.session | run or shared. |
proxy.groups | The configured groups, when set. |
apifyProxy
apifyProxy is a Layer for ProxiedHttpClient, covering every country. Each route call returns an HttpClient whose requests go through the proxy.
Example (A Worker release for QuotesProxiedLive from Adding a proxy)
import { Deployment } from "@scwrap/deploy";
import { apifyProxy } from "@scwrap/platform-apify";
import { Quotes, QuotesProxiedLive } from "@scwrap/quotes";
export default Deployment.worker({
implementation: QuotesProxiedLive,
module: import.meta.url,
scraper: Quotes,
services: apifyProxy,
});A Scraper never silently runs without the proxy it asked for: a request the proxy cannot carry fails with an HttpClientError. On an instance the Release's Scraper also needs the proxy:apify grant.
In a Worker
The Worker version holds no proxy settings. Each request goes to the instance's front desk, which checks the Scraper's grant, builds the route's username with the Run's own session and hands the request to the proxy relay, a small Node Container. The relay opens the CONNECT tunnel and speaks TLS with the site's own name, which a Worker cannot do: startTls sends no SNI (workerd #6903). The Release never sees the password or the relay (Credentials).
- The relay reads the whole request body before it sends it, and the site's response streams back. Compressed responses arrive decoded.
- Outside an instance, in a test or a script, every request fails with a transport error that names the Node version. Tests use
FakeSiteor provideInstanceCredentialsthemselves.
In Node
apifyProxy from @scwrap/platform-apify/node sends requests through Node's HTTP agent with the proxy as HTTP_PROXY and HTTPS_PROXY. Use it in scripts and tests that run in Node.
import { apifyProxy } from "@scwrap/platform-apify/node";
Scraper.run(Quotes, { maxPages: 1 }).pipe(
Stream.runCollect,
Effect.provide(QuotesProxiedLive.pipe(Layer.provide(apifyProxy)))
);Source
- proxy.ts: the settings and the route
- worker.ts: the Worker version
- credentials.ts and proxy-relay.ts: the front desk's proxy capability and the relay
- node.ts: the Node version
- proxy.test.ts: the Node version against a local proxy, and the Worker version against a fake front desk
- proxy-relay.test.ts: the relay tunnelling HTTPS to an origin that checks SNI