JavaScript Slack Incoming Webhook API Example
JavaScript Slack incoming webhook example using fetch. Learn how to send JSON payloads to Slack channels securely and test webhook requests in Apicurl.
Ready to test this code?
Code Example
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL || "https://hooks.slack.com/services/xxx/yyy/zzz";
async function sendSlackMessage(text) {
const response = await fetch(SLACK_WEBHOOK_URL, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ text })
});
if (!response.ok) {
console.error("Slack webhook error", response.status, await response.text());
throw new Error(`Slack webhook failed with status ${response.status}`);
}
console.log("Message sent to Slack");
}
sendSlackMessage("Deployment finished successfully ✅").catch(console.error);
Overview
JavaScript Slack Incoming Webhook API Example
Slack incoming webhooks are a simple way to send messages into channels from your applications, CI pipelines, and backend services. This example shows how to call a Slack webhook from JavaScript and how to test the request in Apicurl.
You will learn how to:
- Create a Slack incoming webhook URL
- Send JSON payloads with text, attachments, and blocks
- Keep the webhook URL secret using environment variables
- Reuse the same request inside Apicurl to debug your payload
1. Create a Slack Incoming Webhook URL
- Go to your Slack workspace and open App Directory.
- Search for Incoming Webhooks or create a custom Slack app.
- Enable Incoming Webhooks for your workspace.
- Create a new webhook for a specific channel.
- Copy the URL, which will look like:
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
Treat this URL like a secret. Anyone with this URL can send messages into the channel.
2. Store the Webhook URL Securely
In your development environment, store the URL in an environment variable:
export SLACK_WEBHOOK_URL="https://hooks.slack.com/services/xxx/yyy/zzz"
In Windows PowerShell:
$Env:SLACK_WEBHOOK_URL="https://hooks.slack.com/services/xxx/yyy/zzz"
In JavaScript (Node.js):
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL || "https://hooks.slack.com/services/xxx/yyy/zzz";
3. Minimal JavaScript Webhook Call with Fetch
Here is a simple example that sends a plain text message using fetch:
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL || "https://hooks.slack.com/services/xxx/yyy/zzz"; async function sendSlackMessage(text) { const response = await fetch(SLACK_WEBHOOK_URL, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ text }) }); if (!response.ok) { console.error("Slack webhook error", response.status, await response.text()); throw new Error(`Slack webhook failed with status ${response.status}`); } console.log("Message sent to Slack"); } sendSlackMessage("Deployment finished successfully ✅").catch(console.error);
This is all you need for basic notifications from scripts and CI pipelines.
4. Sending Rich Messages with Blocks
Slack's Block Kit lets you build more expressive messages. You can send a payload with blocks instead of just text:
async function sendDeploymentSummary(status) { const response = await fetch(SLACK_WEBHOOK_URL, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ text: `Deployment ${status}`, blocks: [ { type: "section", text: { type: "mrkdwn", text: `*Deployment status:* ${status}` } }, { type: "context", elements: [ { type: "mrkdwn", text: "Triggered from Apicurl Slack webhook example" } ] } ] }) }); if (!response.ok) { console.error("Slack webhook error", response.status, await response.text()); throw new Error(`Slack webhook failed with status ${response.status}`); } console.log("Deployment summary sent to Slack"); }
These patterns can be reused to send alerts, metrics summaries, or incident notifications.
5. Using This Example in Apicurl
To design and test your Slack webhook payload in Apicurl:
- Open
/examples/javascript/rest-api/javascript-slack-webhook. - Click "Try It in Apicurl".
- The POST request with a JSON body will load into the Apicurl app.
- Replace the placeholder webhook URL with your real Slack webhook URL.
- Edit the JSON body to match the message you want to send.
- Send the request and verify the message in your Slack channel.
From there you can:
- Save the request as a reusable collection item for deployments or alerts.
- Duplicate it for different channels or message templates.
- Generate code snippets in other languages from the same HTTP configuration.
6. Best Practices Recap
When working with Slack webhooks:
- Keep the webhook URL secret and rotate it if it is ever exposed.
- Use environment variables to store the URL, not hard-coded strings.
- Log failures with both status code and response body when debugging.
- Design and test payloads in Apicurl so you do not have to re-deploy just to adjust JSON.
With this approach, you can quickly wire Slack notifications into any part of your stack using a simple, well-understood HTTP POST.

