How to Configure Telego HTTP Client: FastHTTP vs net/http

You configure Telego’s HTTP client by passing telego.WithFastHTTPClient() for high-performance FastHTTP or telego.WithHTTPClient() for standard net/http flexibility to telego.NewBot(), which sets the internal Caller interface implementation used for all Telegram Bot API requests.

The mymmrac/telego library abstracts all Telegram Bot API network operations behind a Caller interface, allowing you to choose between high-performance FastHTTP or the standard library's net/http. By default, Telego uses FastHTTP, but you can explicitly select either implementation during bot initialization to optimize for raw speed or networking flexibility without changing your bot logic.

Understanding the Caller Interface Architecture

Telego decouples Bot API communication from specific HTTP implementations through the Caller interface defined in telegoapi/caller.go. The library ships with two concrete implementations:

  • FastHTTPCaller (telegoapi/caller.go lines 30-80): Wraps github.com/valyala/fasthttp for minimal allocations and maximum throughput.
  • HTTPCaller (telegoapi/caller.go lines 92-130): Wraps the standard net/http package for broad compatibility with middleware, proxies, and HTTP/2.

The active caller is stored in bot.api and invoked by all API methods like GetMe and SendMessage. Both implementations support the same retry logic via RetryCaller, ensuring consistent error handling regardless of transport choice.

Configuring FastHTTP for High-Performance Bots

To explicitly use FastHTTP or provide a custom configuration, pass WithFastHTTPClient defined in bot_options.go (lines 23-28):

func WithFastHTTPClient(client *fasthttp.Client) BotOption {
    return func(bot *Bot) error {
        bot.api = ta.FastHTTPCaller{Client: client}
        return nil
    }
}

Example: Bot with custom FastHTTP client

package main

import (
	"context"
	"log"
	"os"
	"time"

	"github.com/valyala/fasthttp"
	"github.com/mymmrac/telego"
)

func main() {
	bot, err := telego.NewBot(
		os.Getenv("TOKEN"),
		telego.WithFastHTTPClient(&fasthttp.Client{
			MaxIdleConnDuration: 5 * time.Minute,
			ReadTimeout:         10 * time.Second,
		}),
	)
	if err != nil {
		log.Fatal(err)
	}

	me, err := bot.GetMe(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Bot @%s is running", me.Username)
}

Configuring Standard net/http for Flexibility

For scenarios requiring HTTP/2, proxy support, or custom transports, use WithHTTPClient from bot_options.go (lines 31-36):

func WithHTTPClient(client *http.Client) BotOption {
    return func(bot *Bot) error {
        bot.api = ta.HTTPCaller{Client: client}
        return nil
    }
}

Example: Bot with proxy and TLS configuration

package main

import (
	"context"
	"log"
	"net/http"
	"net/url"
	"os"
	"time"

	"github.com/mymmrac/telego"
)

func main() {
	proxyURL, _ := url.Parse("http://proxy.example.com:3128")
	
	httpClient := &http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyURL(proxyURL),
		},
		Timeout: 15 * time.Second,
	}

	bot, err := telego.NewBot(
		os.Getenv("TOKEN"),
		telego.WithHTTPClient(httpClient),
	)
	if err != nil {
		log.Fatal(err)
	}

	if _, err := bot.GetMe(context.Background()); err != nil {
		log.Fatal(err)
	}
	log.Println("Bot connected successfully via proxy")
}

FastHTTP vs net/http: Performance and Compatibility

Aspect FastHTTP net/http
Performance Zero-allocation, higher throughput for concurrent requests Standard allocations, sufficient for moderate load
HTTP/2 Not supported Native support
Proxy Support Manual implementation required Built-in via http.Transport.Proxy
TLS Configuration Limited options Full control via tls.Config
Memory Usage Lower Higher due to http.Request/Response allocations
Use Case High-frequency polling, webhook servers Corporate proxies, HTTP/2 requirements, complex networking

Both implementations support the RetryCaller wrapper for automatic retries, so switching between them does not require changes to error handling logic.

Implementing a Custom Caller

If neither built-in client meets your requirements, implement the Caller interface and inject it via WithAPICaller:

type Caller interface {
    Call(ctx context.Context, method string, params url.Values, data telegoapi.InputFile) (*telegoapi.Response, error)
}

This approach is ideal for testing with mocks or integrating specialized HTTP libraries that handle specific enterprise requirements.

Summary

  • Telego abstracts HTTP transport through the Caller interface defined in telegoapi/caller.go.
  • FastHTTP (WithFastHTTPClient) provides zero-allocation performance for high-load bots and is the default choice.
  • net/http (WithHTTPClient) offers full compatibility with proxies, HTTP/2, and custom transports for enterprise networks.
  • Both implementations are configured via BotOption functions in bot_options.go during NewBot initialization.
  • You can implement custom transports by satisfying the Caller interface and using WithAPICaller.

Frequently Asked Questions

How do I switch from FastHTTP to net/http in an existing Telego bot?

Replace telego.WithFastHTTPClient(...) with telego.WithHTTPClient(&http.Client{...}) in your telego.NewBot call. All subsequent API calls like GetMe and SendMessage will automatically use the standard library transport without requiring changes to your business logic.

Does Telego support HTTP/2 for Telegram Bot API calls?

HTTP/2 is only supported when using the net/http client. FastHTTP does not implement HTTP/2, so if you require HTTP/2 features, configure your bot with telego.WithHTTPClient using an http.Client with ForceAttemptHTTP2: true in the transport.

Can I use a proxy with Telego?

Yes, but only with the standard net/http client. Pass a custom http.Client configured with Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)} to telego.WithHTTPClient. FastHTTP requires manual proxy handling and is not recommended for scenarios requiring standard proxy support.

Is FastHTTP the default HTTP client in Telego?

Yes, Telego defaults to FastHTTP (FastHTTPCaller) when you create a bot with telego.NewBot without specifying client options. This provides optimal out-of-the-box performance for typical bot workloads, though you can override it by explicitly passing telego.WithHTTPClient or a custom WithAPICaller.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →