Some sites answer only visitors from one country, or block data-center addresses. A Scraper for such a site must send its requests through a proxy.
In Scwrap the Scraper says where its requests must leave from, and whoever provides the Capability decides how. The "where" is a proxy route, a country code such as { country: "US" }. It is a fact about the site, so it lives in the Scraper's package, not in deployment configuration.
This guide gives quotes a route. The Definition stays as it is, so callers notice nothing.
The Capability
ProxiedHttpClient from @scwrap/domain hands out routed clients:
ProxiedHttpClient.route({ country: "US" })
// Effect<HttpClient, never, Scope | ProxiedHttpClient<"US">>You hand it a route and get back an ordinary HttpClient whose requests leave through that route. The client belongs to the Scope it was opened in, so whatever the provider holds for it, such as a proxy session, is released when that scope closes.
The country is part of the type. CountryCode allows only ISO 3166-1 alpha-2 codes, and the requirement ProxiedHttpClient<"US"> says which country the Scraper needs. A provider covers a set of countries, such as ProxiedHttpClient<"US" | "DE">, and satisfies only requirements inside that set.
One line for the whole Implementation
QuotesApiLive already asks for an HttpClient. ProxiedHttpClient.layerHttpClient provides exactly that, from a route.
Example (src/proxied.ts)
import { ProxiedHttpClient } from "@scwrap/domain";
import { Layer } from "effect";
import { QuotesApiLive } from "./api.js";
export const QuotesProxiedLive = QuotesApiLive.pipe(
Layer.provide(ProxiedHttpClient.layerHttpClient({ country: "US" }))
);None of the scraping code changed. The requirement did:
const QuotesApiLive: Layer.Layer<Rpc.Handler<"quotes">, never, HttpClient.HttpClient>
const QuotesProxiedLive: Layer.Layer<Rpc.Handler<"quotes">, never, ProxiedHttpClient<"US">>QuotesProxiedLive no longer accepts any HTTP client. It needs something that can route to the United States, and the type says so. quotes now has two Implementations of one Definition, and a Release picks one.
Export the new Implementation from src/index.ts:
export { QuotesProxiedLive } from "./proxied.js";A session per Run
layerHttpClient asks for one routed client when the Layer is built. When the Scraper needs more control, call ProxiedHttpClient.route inside the handler instead, where the Run starts:
const client = yield* ProxiedHttpClient.route({ country: "US" });The client then belongs to that Run's Scope, and so does whatever the provider holds for it. Because route returns a plain HttpClient, everything Effect offers for HTTP clients still applies: HttpClient.withCookiesRef for a session's cookies, timeouts, retries.
Test the route
The test provides a fake ProxiedHttpClient that covers only the United States. It records the routes it was asked for and hands back a client that answers from memory.
Example (test/proxied.test.ts)
import { expect, it } from "@effect/vitest";
import { ProxiedHttpClient, Scraper } from "@scwrap/domain";
import type { ProxyRoute } from "@scwrap/domain";
import { Effect, Layer, Ref, Stream } from "effect";
import { HttpClient, HttpClientResponse } from "effect/http";
import { Quotes, QuotesProxiedLive } from "../src/index.js";
const body = JSON.stringify({
has_next: false,
quotes: [{ author: { name: "Jane Austen" }, tags: ["books"], text: "…" }],
});
const upstream = HttpClient.make((request) =>
Effect.succeed(HttpClientResponse.fromWeb(request, new Response(body)))
);
it.effect("sends every request through the United States route", () =>
Effect.gen(function* routed() {
const routes = yield* Ref.make<readonly ProxyRoute[]>([]);
const proxy = ProxiedHttpClient.layer<"US">((route) =>
Ref.update(routes, (asked) => [...asked, route]).pipe(
Effect.as(upstream)
)
);
const quotes = yield* Scraper.run(Quotes, { maxPages: 1 }).pipe(
Stream.runCollect,
Effect.provide(QuotesProxiedLive.pipe(Layer.provide(proxy)))
);
expect(quotes).toHaveLength(1);
expect(yield* Ref.get(routes)).toStrictEqual([{ country: "US" }]);
})
);The test proves two things at run time: the Scraper still returns quotes, and it asked for the United States route exactly once. A provider that ignores the route, or an Implementation that bypasses the proxy, fails it. The compiler checks the third: an Implementation that switched to { country: "DE" } would require ProxiedHttpClient<"DE">, which this fake does not provide.
Provide a real proxy
A provider is a Layer for ProxiedHttpClient. The SDKs include one, apifyProxy, which routes through Apify Proxy and covers every country. @scwrap/platform-apify exports it for Workers, and @scwrap/platform-apify/node for Node.
Example (Running through Apify Proxy from Node)
import { Scraper } from "@scwrap/domain";
import { apifyProxy } from "@scwrap/platform-apify/node";
import { Quotes, QuotesProxiedLive } from "@scwrap/quotes";
import { Console, Effect, Layer, Stream } from "effect";
await Effect.runPromise(
Scraper.run(Quotes, { maxPages: 1 }).pipe(
Stream.runForEach((quote) => Console.log(quote)),
Effect.provide(QuotesProxiedLive.pipe(Layer.provide(apifyProxy)))
)
);apifyProxy reads the proxy password from APIFY_PROXY_PASSWORD and, optionally, the proxy groups from SCWRAP_PROXY_GROUPS, for example RESIDENTIAL.
Releasing it
A Release for QuotesProxiedLive must provide a ProxiedHttpClient that covers the United States. If you swap the Implementation in releases/src/quotes.ts and keep FetchHttpClient.layer, the Release stops compiling:
Type 'ProxiedHttpClient' is not assignable to type 'HttpClient'.
Missing 'ProxiedHttpClient' in the expected Layer context. effect(missingLayerContext)That error is the system working. The Scraper says it needs a proxy route to the United States, and a Release that cannot give it one, or provides a proxy for other countries only, is rejected before anything is deployed.
Provide apifyProxy from @scwrap/platform-apify instead. Add @scwrap/platform-apify, at the version of your other SDKs, to releases/package.json.
Example (releases/src/quotes.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,
});In the release Worker, apifyProxy holds no proxy settings. It hands each request to the instance's front desk, which keeps the Apify Proxy password, checks that the Scraper holds the proxy:apify grant and sends the request through its proxy relay. The instance owner grants it in SCWRAP_INSTANCE_GRANTS:
SCWRAP_INSTANCE_GRANTS='{"quotes": ["proxy:apify"]}'What you have now
| Implementation | Needs | Runs as |
|---|---|---|
QuotesApiLive | HttpClient | A Worker release, deployed in the previous guide. |
QuotesProxiedLive | ProxiedHttpClient<"US"> | In tests, and in Node scripts with apifyProxy from @scwrap/platform-apify/node. A Worker release with apifyProxy from @scwrap/platform-apify, once its Scraper holds the proxy:apify grant. |
Next
Adding a browser writes a third Implementation for pages that only render with JavaScript, and releases it as a Worker release with a browser.