Learn how to list chats of a WhatsApp account and send a message in one of them using Unipile SDK.
Prerequisites
Before we start, you need to:
-
Create an Application in the dashboard.
-
Create an API Key from the dashboard.
-
(Optional) Install the SDK & Setup Client
Get the Unipile SDK.npm i @unipile/sdkpnpm add @unipile/sdkInitialize the SDK and prepare request options with the Account API Key you created. Keep the key on your server; replace the placeholder locally.
import { UnipileMessaging } from '@unipile/sdk'; const request_options = { baseUrl: 'https://api.unipile.com', headers: { 'X-API-KEY': 'YOUR_ACCOUNT_API_KEY' }, throwOnError: true, }; const messagingApi = new UnipileMessaging();import unipile configuration = unipile.Configuration() configuration.api_key["apiKey"] = "apikey" api_client = unipile.ApiClient(configuration) messaging_api = unipile.MessagingApi(api_client)
1. Link an account
In the Accounts section of the Dashboard, choose Link Account → Quick Authentication. Use Advanced Authentication when you need custom settings.
Select WhatsApp and authenticate to the account you want to show chats for your demo application.
Once the account is added, keep the given ID as it will be used later.
2. Try to call API Methods
Now, all you have to do is calling the method you want and specify the account ID given in Step 1. In this quickstart guide, we'll see how to list chats and send a message in one of them.
List chats
To get the chats list of the account acc_1234567890, use List all Chats :
const chats = await messagingApi.getChatsList({
...request_options,
path: {
account_id: 'acc_1234567890',
},
});chats = messaging_api.get_chats_list("acc_1234567890")curl --request GET \
--url https://api.unipile.com/v2/acc_1234567890/chats \
--header 'X-API-KEY: api-key' \
--header 'accept: application/json'Send a message in an existing chat
To send a message in an existing chat, use Send a Message and specify the ID of the chat you want to send the message in.
The chat_id can be found in the previously retrieved list.
await messagingApi.sendMessage({
...request_options,
path: {
account_id: 'acc_1234567890',
chat_id: '[email protected]',
},
body: {
text: 'Hello from Unipile!',
},
});messaging_api.send_message(
"[email protected]",
"acc_1234567890",
{
"text": "Hello from Unipile!",
},
)curl --request POST \
--url https://api.unipile.com/v2/acc_1234567890/chats/[email protected]/messages/send \
--header 'X-API-KEY: api-key' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '
{
"text": "Hello from Unipile!"
}
'
Learn once, integrate everything !You can reuse the same Methods with accounts of other providers that support Messaging features !
3. Discover Unipile's fundamentals
Updated about 13 hours ago