Skip to content

How to get the name of the interlocutor in integrations?#

Contents#

Why is the name not always available?#

Due to WhatsApp's security policy, the username may not be available in some cases:

  • If you write first, and the number is not saved in contacts;
  • If the interlocutor did not respond to your message;
  • If the number does not appear in the chat history (no chat).

In such cases, WhatsApp may not provide profile information, including the name.

How to get the name of the interlocutor?#

GetContacts#

The GetContacts method allows you to get a list of numbers added to contacts and those, with whom you have already had a chat.

  • If there is already a chat with the number or the number is saved in contacts, it will be available through this method;
  • In the response, you can find the name field - this is the name specified by the user in WhatsApp;
  • If the name is not available, only the number or an empty field is returned.

Response structure example:

{
    "id": "79876543210@c.us",
    "name": "Name",
    "contactName": "Contact name",
    "type": "user"
}

Important!

If you want the contact to be displayed when you request GetContacts, you need to:

  • either save the number manually to contacts,
  • or have at least one chat with the number (incoming or outgoing message).

GetContactInfo#

The GetContactInfo method allows you to get data on a specific phone number.

The response may contain:

  • name — profile name (if available),
  • contactName — name specified in the contact book (if the number is saved in contacts),
  • other profile data (avatar, email, etc.)

Response structure example:

{
    "avatar": "https://pps.whatsapp.net/v/...",
    "name": "Name",
    "contactName": "Contact name",
    {...}
}

GetGroupData#

If you work with groups, the GetGroupData method will help you get the name of the group from which the message was received.

Response structure example:

{
    "groupId": "12345678909876543210@g.us",
    "owner": "79876543210@c.us",
    "subject": "GREEN-API Group",
    {...}
}

Handling notifications#

One of the most reliable ways to get the name is to handle incoming notifications.

When you receive an incoming message, the body of the notification may contain a field with the name of the sender. This name is displayed even if you have not saved the contact, provided that the user wrote first.

Notification structure example:

{
    "typeWebhook": "incomingMessageReceived",
    "instanceData": {...},
    "timestamp": 1111111111,
    "idMessage": "F7AEC1B7086ECDC7E6E45923F5EDB825",
    "senderData": {
        "chatId": "79001234567@c.us",
        "sender": "79001234567@c.us",
        "chatName": "Ivan",
        "senderName": "Ivan",
        "senderContactName": "Ivan Tsarevich"
        },
    "messageData": {...}
}

Important!

The name in senderName may not be available if you wrote first and the user did not respond.

How to initiate a chat if there is no name yet?#

If you need to start a conversation with a user who does not have a chat and the contact is not saved:

  • Use sending methods with the number in the chatId format ({number}@c.us).
  • After this, the chat will be created, but the name is not guaranteed to appear - it will be displayed only after the user responds.

Conclusion#

If you are building a CRM system or a bot that should "remember" user names, the following algorithm is recommended:

  1. When receiving an incoming message - save senderName locally;
  2. When sending messages first - do not count on the presence of a name right away;
  3. When receiving a response - update the name in your database.