GREEN-API WhatsApp SDK Library v2
A TypeScript/JavaScript SDK for interacting with the GREEN-API WhatsApp gateway.
Installation
npm install @green-api/whatsapp-api-client-js-v2
yarn add @green-api/whatsapp-api-client-js-v2
API
Documentation for the REST API is located at link. The library is a wrapper for
the REST API,
so the documentation at the link above is also applicable to the library itself.
Authorization
To send a message or perform other GREEN-API methods, the WhatsApp account in the phone application must be in the
authorized state. To authorize the instance, go to console and
scan the QR code using the WhatsApp application.
Getting Started
To use the SDK, you need to create an instance of the GreenApiClient with your GREEN-API instance credentials:
import { GreenApiClient } from '@green-api/whatsapp-api-client-js-v2';
const client = new GreenApiClient({
idInstance: 12345,
apiTokenInstance: 'your-api-token'
});
For Partner API access, use the GreenApiPartnerClient:
import { GreenApiPartnerClient } from '@green-api/whatsapp-api-client-js-v2';
const partnerClient = new GreenApiPartnerClient({
partnerToken: 'your-partner-token',
partnerApiUrl: 'https://api.green-api.com'
});
Usage Examples
Sending a Text Message
await client.sendMessage({
chatId: '1234567890@c.us',
message: 'Hello from GREEN-API SDK!'
});
Sending a File by URL
await client.sendFileByUrl({
chatId: '1234567890@c.us',
file: {
url: 'https://example.com/file.pdf',
fileName: 'document.pdf'
},
caption: 'Check this file'
});
Creating a Poll
await client.sendPoll({
chatId: '1234567890@c.us',
message: 'What\'s your favorite color?',
options: [
{optionName: 'Red'},
{optionName: 'Blue'},
{optionName: 'Green'}
],
multipleAnswers: false
});
Managing Groups
const group = await client.createGroup({
groupName: 'My Test Group',
chatIds: ['1234567890@c.us', '0987654321@c.us']
});
await client.addGroupParticipant({
groupId: group.chatId,
participantChatId: '1122334455@c.us'
});
Receiving Notifications
const notification = await client.receiveNotification(30);
if (notification) {
console.log('Received notification:', notification.body.typeWebhook);
if (notification.body.typeWebhook === 'incomingMessageReceived') {
console.log('Message:', notification.body.messageData);
}
await client.deleteNotification(notification.receiptId);
}
const fileData = await client.downloadFile({
chatId: '1234567890@c.us',
idMessage: 'MESSAGE_ID_WITH_FILE'
});
console.log('File URL:', fileData.downloadUrl);
Working with WhatsApp Statuses (Beta)
await client.sendTextStatus({
message: "Hello from GREEN-API SDK!",
backgroundColor: "#228B22",
font: "SERIF",
participants: ["1234567890@c.us"]
});
await client.sendMediaStatus({
urlFile: "https://example.com/image.jpg",
fileName: "image.jpg",
caption: "Check out this view!",
participants: ["1234567890@c.us"]
});
const stats = await client.getStatusStatistic({
idMessage: "BAE5F4886F6F2D05"
});
console.log(`Status was viewed by ${stats.length} contacts`);
const statuses = await client.getIncomingStatuses({minutes: 60});
statuses.forEach(status => {
console.log(`Status from ${status.senderName} at ${new Date(status.timestamp * 1000)}`);
});
Partner API (Instance Management)
const instances = await partnerClient.getInstances();
console.log(`Total instances: ${instances.length}`);
console.log(`Active instances: ${instances.filter(i => !i.deleted).length}`);
const instance = await partnerClient.createInstance({
name: "Marketing Campaign",
incomingWebhook: "yes",
outgoingWebhook: "yes",
delaySendMessagesMilliseconds: 3000
});
console.log(`Created instance with ID: ${instance.idInstance}`);
console.log(`API Token: ${instance.apiTokenInstance}`);
const result = await partnerClient.deleteInstanceAccount({
idInstance: instance.idInstance
});
if (result.deleteInstanceAccount) {
console.log("Instance successfully deleted");
}
Editing and Deleting Messages
const editResult = await client.editMessage({
chatId: '1234567890@c.us',
idMessage: 'BAE5367237E13A87',
message: 'This is the edited message text'
});
console.log('Edited message ID:', editResult.idMessage);
await client.deleteMessage({
chatId: '1234567890@c.us',
idMessage: 'BAE5F4886F6F2D05'
});
await client.deleteMessage({
chatId: '1234567890@c.us',
idMessage: 'BAE5F4886F6F2D05',
onlySenderDelete: true
});
Working with contacts
const addResult = await client.addContact({
chatId: '1234567890@c.us',
firstName: 'John',
lastName: 'Doe',
saveInAddressbook: true
});
console.log('Add contact result:', addResult.addContact);
const editResult = await client.editContact({
chatId: '1234567890@c.us',
firstName: 'Jane',
lastName: 'Doe',
saveInAddressbook: true
});
console.log('Edit contact result:', editResult.editContact);
const deleteResult = await client.deleteContact({
chatId: '1234567890@c.us'
});
console.log('Delete contact result:', deleteResult.deleteContact);
SDK methods
The SDK provides the following groups of methods:
License
MIT