Skip to content

Apify Proxy

@scwrap/platform-apify provides proxy routes through Apify Proxy: apifyProxy for Worker releases through the instance's front desk, and for Node from @scwrap/platform-apify/node.

@scwrap/platform-apify is the provider behind proxy routes. It turns a Scraper's ProxyRoute into an Apify Proxy connection.

ExportImport fromRuns inProvides
apifyProxy@scwrap/platform-apifyA Worker release on an instanceProxiedHttpClient, through the instance's front desk
apifyProxy@scwrap/platform-apify/nodeNodeProxiedHttpClient, over Node's HTTP agent
apifyProxyEndpoint(route)@scwrap/platform-apifyAnywhereThe 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).

VariableDefaultMeaning
APIFY_PROXY_PASSWORDRequiredThe proxy password of the Apify account.
SCWRAP_PROXY_GROUPSNoneProxy groups, joined with +, for example RESIDENTIAL.
SCWRAP_RUN_IDNoneNames the session of one Run. An instance sets it to each Run's result ID.
ACTOR_RUN_IDlocalNames the shared session, and every session when SCWRAP_RUN_ID is unset.
APIFY_PROXY_HOSTNAMEproxy.apify.comThe proxy host.
APIFY_PROXY_PORT8000The 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.

text
groups-RESIDENTIAL,session-<run id>,country-FR

Requests with the same session leave from the same address. A route's session chooses which one it uses:

sessionSessionUse it when
"run", the defaultSCWRAP_RUN_ID, so each Run that sets it has its own address; ACTOR_RUN_ID when it is unsetRuns should not look like one visitor, or one Run's block should not affect another.
"shared"ACTOR_RUN_ID, the same for every RunRuns should keep one address, for example to reuse a site's trust in it.
ts
ProxiedHttpClient.route({ country: "BR", session: "shared" });

It also annotates the current span, so a trace shows where a request went out:

Span attributeValue
proxy.providerapify-proxy
proxy.countryThe route's country, when it has one.
proxy.sessionrun or shared.
proxy.groupsThe 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)

ts
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 FakeSite or provide InstanceCredentials themselves.

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.

ts
import { apifyProxy } from "@scwrap/platform-apify/node";

Scraper.run(Quotes, { maxPages: 1 }).pipe(
  Stream.runCollect,
  Effect.provide(QuotesProxiedLive.pipe(Layer.provide(apifyProxy)))
);

Source ​