Reduce bandwidth
A session can block the parts of a page that your task does not need, such as ads, images and videos, so that pages load faster and less data goes through the proxy. This matters most with Steel proxies, because Steel bills them by the bytes that they carry.
Before you begin
Put your Steel API key in the STEEL_API_KEY environment variable, and install the Steel SDK for your language: TypeScript, Python, Go or Rust.
Block ads
A session blocks ads by default. When the request leaves out blockAds, Steel sets it to true, and the browser refuses requests to a list of known ad and tracking hosts and their subdomains.
If the task needs those requests, set blockAds to false:
const session = await client.sessions.create({ blockAds: false });session = client.sessions.create(block_ads=False)session, err := client.Sessions.New(ctx, steel.SessionNewParams{
BlockAds: steel.F(false),
})
if err != nil {
return err
}let session = client
.sessions()
.create(steel_rs::SessionCreateParams {
block_ads: Some(false),
..Default::default()
})
.await?;Turn on bandwidth optimization
Set optimizeBandwidth to true to block images, media and stylesheets together.
This program creates a session with bandwidth optimization, prints what the session blocks, and releases it.
import Steel from "steel-sdk";
const client = new Steel();
const session = await client.sessions.create({ optimizeBandwidth: true });
try {
console.log(JSON.stringify(session.optimizeBandwidth, null, 2));
} finally {
await client.sessions.release(session.id);
}import json
from steel import Steel
client = Steel()
session = client.sessions.create(optimize_bandwidth=True)
try:
print(json.dumps(session.optimize_bandwidth.to_dict(mode="json"), indent=2))
finally:
client.sessions.release(session.id)package main
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/steel-dev/sdk/go"
"github.com/steel-dev/sdk/go/shared"
)
func main() {
if err := run(context.Background()); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
client := steel.NewClient()
session, err := client.Sessions.New(ctx, steel.SessionNewParams{
OptimizeBandwidth: steel.F[steel.SessionNewParamsOptimizeBandwidthUnion](shared.UnionBool(true)),
})
if err != nil {
return err
}
defer func() {
if _, err := client.Sessions.Release(ctx, session.ID); err != nil {
log.Print(err)
}
}()
out, err := json.MarshalIndent(session.OptimizeBandwidth, "", " ")
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = steel_rs::Client::new();
let session = client
.sessions()
.create(steel_rs::SessionCreateParams {
optimize_bandwidth: Some(steel_rs::SessionCreateParamsOptimizeBandwidth::Bool(true)),
..Default::default()
})
.await?;
let result: Result<(), Box<dyn std::error::Error>> = async {
println!(
"{}",
serde_json::to_string_pretty(&session.optimize_bandwidth)?
);
Ok(())
}
.await;
client.sessions().release(&session.id).await?;
result
}In Rust, the program also needs cargo add serde_json. Run the program:
npx tsx bandwidth.mtspython bandwidth.pygo run .cargo run{
"blockImages": true,
"blockMedia": true,
"blockStylesheets": true
}{
"blockImages": true,
"blockMedia": true,
"blockStylesheets": true
}{
"blockHosts": null,
"blockImages": true,
"blockMedia": true,
"blockStylesheets": true,
"blockUrlPatterns": null
}{
"blockImages": true,
"blockMedia": true,
"blockStylesheets": true
}true does not block any host or URL pattern. To block those, pass an object, as the next section shows.
Choose what to block
Pass an object in optimizeBandwidth to choose each kind of resource:
| Field | What the session blocks |
|---|---|
blockImages | Images. |
blockMedia | Video and audio. |
blockStylesheets | Stylesheets. |
blockHosts | Every request to a host in the list, and to its subdomains. |
blockUrlPatterns | Every request whose URL matches a pattern in the list. A pattern must match the whole URL, including the query string. * matches any characters, and the match ignores case. |
This session keeps stylesheets, and blocks images, media, one analytics host and every .woff2 font:
const session = await client.sessions.create({
optimizeBandwidth: {
blockImages: true,
blockMedia: true,
blockHosts: ["analytics.example.com"],
blockUrlPatterns: ["*.woff2*"],
},
});session = client.sessions.create(
optimize_bandwidth={
"block_images": True,
"block_media": True,
"block_hosts": ["analytics.example.com"],
"block_url_patterns": ["*.woff2*"],
},
)session, err := client.Sessions.New(ctx, steel.SessionNewParams{
OptimizeBandwidth: steel.F[steel.SessionNewParamsOptimizeBandwidthUnion](steel.SessionNewParamsOptimizeBandwidthUnionMember1{
BlockImages: steel.F(true),
BlockMedia: steel.F(true),
BlockHosts: steel.F([]string{"analytics.example.com"}),
BlockURLPatterns: steel.F([]string{"*.woff2*"}),
}),
})
if err != nil {
return err
}let session = client
.sessions()
.create(steel_rs::SessionCreateParams {
optimize_bandwidth: Some(
steel_rs::SessionCreateParamsOptimizeBandwidth::UnionMember1(
steel_rs::SessionCreateParamsOptimizeBandwidthUnionMember1 {
block_images: Some(true),
block_media: Some(true),
block_hosts: Some(vec!["analytics.example.com".to_owned()]),
block_url_patterns: Some(vec!["*.woff2*".to_owned()]),
..Default::default()
},
),
),
..Default::default()
})
.await?;The * at the end of *.woff2* lets the pattern match a font URL with a query string, such as https://example.com/inter.woff2?v=3.
Block only what the task does not use:
- If the task takes screenshots, or a model reads the page as an image, keep images.
- If the task depends on the layout, or on whether an element is visible, keep stylesheets.
A session's proxyBytesUsed shows how many bytes went through its proxy, and Session cost shows the bytes that Steel billed.
Check that it worked
Run the same task twice on a Steel proxy: once in a session with optimizeBandwidth set to true, and once in a session without it. After you release both sessions, read their costs with sessions.cost, as Session cost shows. On a page with images or video, the session that blocks them has the smaller usage.proxy.bytes.