Sticker Messages
Sticker messages allow you to send static or animated stickers to your recipients through WhatsApp. Stickers are WebP images that can be static or animated and are a fun, visual way to engage with users.
Basic Usage
import WhatsApp from 'meta-cloud-api';
// Initialize client
const whatsapp = new WhatsApp({
phoneNumberId: YOUR_PHONE_NUMBER_ID,
accessToken: 'YOUR_ACCESS_TOKEN'
});
// Send sticker message using a URL
const response = await whatsapp.messages.sticker(
{
link: "https://example.com/sticker.webp"
},
15551234567
);
console.log(`Sticker message sent with ID: ${response.data.messages[0].id}`);
Parameters
The sticker()
method accepts the following parameters:
Parameter | Type | Description |
---|---|---|
message | object | The sticker message object |
recipient | string or number | The recipient's phone number with country code |
replyMessageId | string (optional) | ID of a message to reply to |
Message Object Properties
Property | Type | Description | Required |
---|---|---|---|
link | string | URL of the sticker file (WebP format) | Required if id is not provided |
id | string | Media ID of a previously uploaded sticker | Required if link is not provided |
Examples
Sending a Static Sticker with a URL
const response = await whatsapp.messages.sticker(
{
link: "https://example.com/static-sticker.webp"
},
15551234567
);
Sending an Animated Sticker with a URL
const response = await whatsapp.messages.sticker(
{
link: "https://example.com/animated-sticker.webp"
},
15551234567
);
Sending a Sticker with a Media ID
If you've previously uploaded a sticker and have its Media ID:
const response = await whatsapp.messages.sticker(
{
id: "1234567890"
},
15551234567
);
Replying with a Sticker
const originalMessageId = "wamid.abcd1234...";
const response = await whatsapp.messages.sticker(
{
link: "https://example.com/response-sticker.webp"
},
15551234567,
originalMessageId
);
Uploading Stickers
Before sending stickers with a Media ID, you need to upload them:
// Upload a static sticker file
const uploadResponse = await whatsapp.media.upload({
file: "/path/to/local/sticker.webp",
type: "image/webp"
});
// Get the media ID from the response
const mediaId = uploadResponse.data.id;
// Now send the sticker using the media ID
const messageResponse = await whatsapp.messages.sticker(
{
id: mediaId
},
15551234567
);
Sticker Requirements
WhatsApp has specific requirements for stickers:
Static Stickers
- Format: WebP
- Maximum file size: 100 KB
- Dimensions: 512x512 pixels recommended
Animated Stickers
- Format: WebP (animated)
- Maximum file size: 500 KB
- Dimensions: 512x512 pixels recommended
- Animation duration: 3 seconds or less
Creating Stickers
You can create WebP stickers using various tools:
-
Converting from PNG/JPG: You can use tools like cwebp (from the libwebp package) to convert static images:
cwebp -q 80 input.png -o output.webp
-
Creating animated WebP files: Use tools like gif2webp to convert animated GIFs:
gif2webp -lossy input.gif -o output.webp
-
Online converters: Various online services can convert images to WebP format suitable for WhatsApp stickers.
Error Handling
try {
const response = await whatsapp.messages.sticker(
{
link: "https://example.com/sticker.webp"
},
15551234567
);
console.log("Sticker message sent successfully:", response.data);
} catch (error) {
console.error("Error sending sticker message:", error);
// Handle specific error cases
if (error.response && error.response.data) {
if (error.response.data.error.code === 131053) {
console.log("Media URL is not accessible or supported");
} else if (error.response.data.error.code === 131052) {
console.log("Media file size too big. Check size limits for stickers");
} else {
console.log("Error details:", error.response.data);
}
}
}
Best Practices
-
Optimize sticker size: Keep static stickers under 100 KB and animated stickers under 500 KB.
-
Use transparent backgrounds: Stickers with transparent backgrounds look better and more professional.
-
Keep designs simple: Stickers with simple, bold designs are more effective than complex imagery.
-
Create themed sticker packs: If sending multiple stickers, consider creating thematically related sets.
-
Use animation effectively: For animated stickers, keep the animation smooth, simple, and meaningful.
-
Test on mobile devices: Ensure your stickers display well on a variety of mobile screens.
Related
- Media API - For uploading and managing media
- Image Messages - For sending regular images
- Interactive Messages - For creating interactive elements