setWebhook
Sets a webhook.
Returns: On success, returns True.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
url | String | Yes | Webhook URL (empty string = delete) |
certificate | InputFile | Optional | Public key certificate (for self-signed) |
ip_address | String | Optional | IP address for webhook |
max_connections | Integer | Optional | Max connections (1-100, default 40) |
allowed_updates | Array of String | Optional | JSON array of update types |
drop_pending_updates | Boolean | Optional | Drop pending updates |
secret_token | String | Optional | Secret token for X-Telegram-Bot-Api-Secret-Token |
Example request
curl
curl -s -X POST "https://api.telegram.org/bot$TOKEN/setWebhook" \
-H "Content-Type: application/json" \
-d '{"url":"https://example.com/telegram/webhook"}'
Python
import requests
TOKEN = "123456789:AAEexampleTokenReplaceWithYours"
resp = requests.post(
f"https://api.telegram.org/bot{TOKEN}/setWebhook",
json={
"url": "https://example.com/telegram/webhook"
},
timeout=30,
)
print(resp.json())
JavaScript
const TOKEN = "123456789:AAEexampleTokenReplaceWithYours";
const res = await fetch(`https://api.telegram.org/bot${TOKEN}/setWebhook`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
"url": "https://example.com/telegram/webhook"
}),
});
console.log(await res.json());
Step-by-step guide: How to Set Up a Telegram Bot Webhook: setWebhook, Secret Token and Troubleshooting
Related methods
getWebhookInfoReturns webhook info.deleteWebhookDeletes a webhook.getUpdatesGets updates. Use the 'Start' button for long polling.