|
| 1 | +--- |
| 2 | +title: "Configure Proxy" |
| 3 | +--- |
| 4 | + |
| 5 | +Kernel browsers can be configured to route traffic through proxies for enhanced privacy, geographic targeting, and bot detection avoidance. You can either create reusable proxy configurations or use them directly with browsers. |
| 6 | + |
| 7 | +## Using proxies with browsers |
| 8 | + |
| 9 | +To use a proxy with a browser, specify the `proxy_id` parameter when creating the browser session: |
| 10 | + |
| 11 | +<CodeGroup> |
| 12 | + |
| 13 | +```typescript Typescript/Javascript |
| 14 | +import { Kernel } from '@onkernel/sdk'; |
| 15 | +const kernel = new Kernel(); |
| 16 | + |
| 17 | +// First, create a proxy configuration |
| 18 | +const proxy = await kernel.proxies.create({ |
| 19 | + type: 'residential', |
| 20 | + name: 'US Residential', |
| 21 | + config: { |
| 22 | + country: 'US' |
| 23 | + } |
| 24 | +}); |
| 25 | + |
| 26 | +// Then use it with a browser |
| 27 | +const browser = await kernel.browsers.create({ |
| 28 | + proxy_id: proxy.id, |
| 29 | + stealth: true // Recommended when using proxies |
| 30 | +}); |
| 31 | +``` |
| 32 | + |
| 33 | +```Python Python |
| 34 | +import kernel |
| 35 | +client = kernel.Kernel() |
| 36 | + |
| 37 | +# First, create a proxy configuration |
| 38 | +proxy = client.proxies.create( |
| 39 | + type='residential', |
| 40 | + name='US Residential', |
| 41 | + config={ |
| 42 | + 'country': 'US' |
| 43 | + } |
| 44 | +) |
| 45 | + |
| 46 | +# Then use it with a browser |
| 47 | +browser = client.browsers.create( |
| 48 | + proxy_id=proxy.id, |
| 49 | + stealth=True # Recommended when using proxies |
| 50 | +) |
| 51 | +``` |
| 52 | + |
| 53 | +</CodeGroup> |
| 54 | + |
| 55 | +## Reusing proxy configurations |
| 56 | + |
| 57 | +Once created, proxy configurations can be reused across multiple browser sessions: |
| 58 | + |
| 59 | +<CodeGroup> |
| 60 | + |
| 61 | +```typescript Typescript/Javascript |
| 62 | +// Get existing proxies |
| 63 | +const proxies = await kernel.proxies.list(); |
| 64 | +const usProxy = proxies.find(p => p.name === 'US Residential'); |
| 65 | + |
| 66 | +// Create multiple browsers with the same proxy |
| 67 | +const browsers = []; |
| 68 | +for (let i = 0; i < 5; i++) { |
| 69 | + const browser = await kernel.browsers.create({ |
| 70 | + proxy_id: usProxy.id |
| 71 | + }); |
| 72 | + browsers.push(browser); |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +```Python Python |
| 77 | +# Get existing proxies |
| 78 | +proxies = client.proxies.list() |
| 79 | +us_proxy = next(p for p in proxies if p.name == 'US Residential') |
| 80 | + |
| 81 | +# Create multiple browsers with the same proxy |
| 82 | +browsers = [] |
| 83 | +for i in range(5): |
| 84 | + browser = client.browsers.create( |
| 85 | + proxy_id=us_proxy.id |
| 86 | + ) |
| 87 | + browsers.append(browser) |
| 88 | +``` |
| 89 | + |
| 90 | +</CodeGroup> |
| 91 | + |
| 92 | +## Best practices |
| 93 | + |
| 94 | +### 1. Combine with stealth mode |
| 95 | + |
| 96 | +When using proxies, always enable stealth mode for better bot detection avoidance: |
| 97 | + |
| 98 | +<CodeGroup> |
| 99 | + |
| 100 | +```typescript Typescript/Javascript |
| 101 | +const browser = await kernel.browsers.create({ |
| 102 | + proxy_id: proxy.id, |
| 103 | + stealth: true |
| 104 | +}); |
| 105 | +``` |
| 106 | + |
| 107 | +```Python Python |
| 108 | +browser = client.browsers.create( |
| 109 | + proxy_id=proxy.id, |
| 110 | + stealth=True |
| 111 | +) |
| 112 | +``` |
| 113 | + |
| 114 | +</CodeGroup> |
| 115 | + |
| 116 | +### 2. Match proxy type to use case |
| 117 | + |
| 118 | +Choose the appropriate proxy type based on your needs: |
| 119 | + |
| 120 | +- **Mobile**: Best for avoiding detection, slowest |
| 121 | +- **Residential**: Great for avoiding detection, moderate speed |
| 122 | +- **ISP**: Good balance of speed and detection avoidance |
| 123 | +- **Datacenter**: Fastest but most easily detected |
| 124 | +- **Custom**: Use your own proxy infrastructure |
| 125 | + |
| 126 | +### 3. Geographic consistency |
| 127 | + |
| 128 | +Ensure proxy location matches your browser profile's location: |
| 129 | + |
| 130 | +<CodeGroup> |
| 131 | + |
| 132 | +```typescript Typescript/Javascript |
| 133 | +// Create a profile with US location |
| 134 | +const profile = await kernel.profiles.create({ |
| 135 | + name: 'US User', |
| 136 | + locale: 'en-US', |
| 137 | + timezone: 'America/New_York' |
| 138 | +}); |
| 139 | + |
| 140 | +// Use a US proxy to match |
| 141 | +const proxy = await kernel.proxies.create({ |
| 142 | + type: 'residential', |
| 143 | + config: { |
| 144 | + country: 'US', |
| 145 | + state: 'NY' |
| 146 | + } |
| 147 | +}); |
| 148 | + |
| 149 | +// Create browser with matching profile and proxy |
| 150 | +const browser = await kernel.browsers.create({ |
| 151 | + profile_id: profile.id, |
| 152 | + proxy_id: proxy.id |
| 153 | +}); |
| 154 | +``` |
| 155 | + |
| 156 | +```Python Python |
| 157 | +# Create a profile with US location |
| 158 | +profile = client.profiles.create( |
| 159 | + name='US User', |
| 160 | + locale='en-US', |
| 161 | + timezone='America/New_York' |
| 162 | +) |
| 163 | + |
| 164 | +# Use a US proxy to match |
| 165 | +proxy = client.proxies.create( |
| 166 | + type='residential', |
| 167 | + config={ |
| 168 | + 'country': 'US', |
| 169 | + 'state': 'NY' |
| 170 | + } |
| 171 | +) |
| 172 | + |
| 173 | +# Create browser with matching profile and proxy |
| 174 | +browser = client.browsers.create( |
| 175 | + profile_id=profile.id, |
| 176 | + proxy_id=proxy.id |
| 177 | +) |
| 178 | +``` |
| 179 | + |
| 180 | +</CodeGroup> |
| 181 | + |
| 182 | +## Proxy rotation strategies |
| 183 | + |
| 184 | +### Sequential rotation |
| 185 | + |
| 186 | +Rotate through proxies in order: |
| 187 | + |
| 188 | +<CodeGroup> |
| 189 | + |
| 190 | +```typescript Typescript/Javascript |
| 191 | +const proxies = await kernel.proxies.list(); |
| 192 | +let currentIndex = 0; |
| 193 | + |
| 194 | +function getNextProxy() { |
| 195 | + const proxy = proxies[currentIndex]; |
| 196 | + currentIndex = (currentIndex + 1) % proxies.length; |
| 197 | + return proxy; |
| 198 | +} |
| 199 | + |
| 200 | +// Use different proxy for each task |
| 201 | +for (const task of tasks) { |
| 202 | + const proxy = getNextProxy(); |
| 203 | + const browser = await kernel.browsers.create({ |
| 204 | + proxy_id: proxy.id |
| 205 | + }); |
| 206 | + await performTask(browser, task); |
| 207 | + await browser.close(); |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +```Python Python |
| 212 | +proxies = client.proxies.list() |
| 213 | +current_index = 0 |
| 214 | + |
| 215 | +def get_next_proxy(): |
| 216 | + global current_index |
| 217 | + proxy = proxies[current_index] |
| 218 | + current_index = (current_index + 1) % len(proxies) |
| 219 | + return proxy |
| 220 | + |
| 221 | +# Use different proxy for each task |
| 222 | +for task in tasks: |
| 223 | + proxy = get_next_proxy() |
| 224 | + browser = client.browsers.create( |
| 225 | + proxy_id=proxy.id |
| 226 | + ) |
| 227 | + await perform_task(browser, task) |
| 228 | + browser.close() |
| 229 | +``` |
| 230 | + |
| 231 | +</CodeGroup> |
| 232 | + |
| 233 | +### Random rotation |
| 234 | + |
| 235 | +Randomly select proxies for better distribution: |
| 236 | + |
| 237 | +<CodeGroup> |
| 238 | + |
| 239 | +```typescript Typescript/Javascript |
| 240 | +const proxies = await kernel.proxies.list(); |
| 241 | + |
| 242 | +function getRandomProxy() { |
| 243 | + const index = Math.floor(Math.random() * proxies.length); |
| 244 | + return proxies[index]; |
| 245 | +} |
| 246 | + |
| 247 | +const browser = await kernel.browsers.create({ |
| 248 | + proxy_id: getRandomProxy().id |
| 249 | +}); |
| 250 | +``` |
| 251 | + |
| 252 | +```Python Python |
| 253 | +import random |
| 254 | + |
| 255 | +proxies = client.proxies.list() |
| 256 | + |
| 257 | +def get_random_proxy(): |
| 258 | + return random.choice(proxies) |
| 259 | + |
| 260 | +browser = client.browsers.create( |
| 261 | + proxy_id=get_random_proxy().id |
| 262 | +) |
| 263 | +``` |
| 264 | + |
| 265 | +</CodeGroup> |
| 266 | + |
| 267 | +## Troubleshooting |
| 268 | + |
| 269 | +### Proxy connection failures |
| 270 | + |
| 271 | +If a browser fails to connect through a proxy: |
| 272 | + |
| 273 | +1. Verify the proxy configuration is correct |
| 274 | +2. Check if the proxy type supports your target site |
| 275 | +3. Try a different proxy type or location |
| 276 | +4. Ensure custom proxies are accessible |
| 277 | + |
| 278 | +### Performance issues |
| 279 | + |
| 280 | +If experiencing slow performance: |
| 281 | + |
| 282 | +1. Consider using faster proxy types (ISP or datacenter) |
| 283 | +2. Choose proxy locations closer to target servers |
| 284 | +3. Reduce the number of concurrent browsers per proxy |
| 285 | +4. Monitor proxy health and rotate out slow proxies |
| 286 | + |
| 287 | +## Related resources |
| 288 | + |
| 289 | +- [Proxies Overview](/proxies/overview) - Learn about proxy types and capabilities |
| 290 | +- [Stealth Mode](/browsers/stealth) - Enhance bot detection avoidance |
| 291 | +- [Browser Profiles](/browsers/profiles) - Create consistent browser identities |
0 commit comments