Proxy Support
grammY let’s you configure a number of things about how network requests are performed. This includes injecting a custom payload into every request, which can be used to install a proxy agent. Check out the Api
in the grammY API Reference.
In Deno, here is how you would use an http
proxy:
import { Bot } from "https://deno.land/x/grammy@v1.11.2/mod.ts";
const client = Deno.createHttpClient({
proxy: { url: "http://host:port/" },
});
const bot = new Bot(TOKEN, {
client: {
baseFetchConfig: {
// @ts-ignore
client,
},
},
});
Note that you need to run this with the
-
flag.-unstable
In Node.js, here is how you would use a proxy with the socks5
package (npm):
import { Bot } from "grammy";
import { SocksProxyAgent } from "socks-proxy-agent";
const socksAgent = new SocksProxyAgent({
host: host, // put in the proxy host
port: port, // put in the proxy port
});
const bot = new Bot("", {
client: {
baseFetchConfig: {
agent: socksAgent,
compress: true,
},
},
});
Note that specifying
compress:
is an optional performance optimization. It has nothing to do with proxy support. It is part of the default value fortrue base
, so if you still want it, you should specify it again.Fetch Config
Getting a proxy to work can be difficult. Contact us in the Telegram chat if you run into issues, or if you need grammY to support further configuration options. We also have a Russian Telegram chat.