Skip to main content
Webhooks are the preferred way to receive updates in production. Instead of polling Telegram’s servers, Telegram sends updates directly to your server via HTTP POST requests.

Why Webhooks?

Lower Latency

Updates arrive instantly without polling delays

Lower Load

No constant connections to Telegram’s servers

Scalable

Works well with serverless platforms and load balancers

Cost-Effective

Reduced bandwidth and compute costs

Basic Setup

Never expose your bot token in the URL! Use it as a secret path component to prevent unauthorized webhook calls.

Framework Integration

grammY supports many web frameworks:

Serverless Platforms

Cloudflare Workers

Deno Deploy

Vercel

Webhook Configuration

Security

Secret Token Verification

HTTPS Requirement

Telegram only sends webhooks to HTTPS URLs. For local development, use tunneling tools like ngrok, Cloudflare Tunnel, or Serveo.

Local Development with Tunneling

Using ngrok

Then set the webhook:

Health Checks

Managing Webhooks

Error Handling

Best Practices

Use Secret Tokens

Always verify webhook requests with secret tokens

Handle Gracefully

Return 200 OK even if your handler fails to prevent retries

Keep URLs Secret

Use your bot token as part of the webhook path

Monitor Webhook Info

Regularly check getWebhookInfo() for errors

Troubleshooting

  • Verify your URL is accessible from the internet
  • Check that you’re using HTTPS with a valid certificate
  • Ensure your server responds quickly (under 60 seconds)
  • Check getWebhookInfo() for error messages
  • Use a valid SSL certificate from a trusted CA
  • Self-signed certificates must be uploaded via setWebhook
  • Check certificate expiration
  • Check your max_connections setting
  • Ensure your server responds within 60 seconds
  • Monitor webhook info for pending update count
  • Consider scaling your infrastructure

See Also