A "Chat on WhatsApp" button looks like the easiest feature on the backlog. It's one URL. Then the bug reports come in: the link opens a "phone number shared via url is invalid" screen, or the chat opens with half the prefilled message missing. Almost every time, the cause is one of two things: the number format or the URL encoding.

The shape of the URL

A click-to-chat link has two parts:

https://wa.me/<number>?text=<url-encoded message> 

The ?text= part is optional. The number is not, and it's the part people get wrong.

Rule 1: the number is E.164 without the plus

WhatsApp wants the full international number as digits only: country code first, then the national number. No +, no 00 international prefix, no spaces, dashes or brackets.

A US number written as +1 (202) 555-0123 becomes:

https://wa.me/12025550123 

Two traps hide here. First, 00 is a dialing prefix, not part of the number, so 0044... has to become 44.... Second, many countries use a leading trunk 0 for domestic calls. A UK mobile written locally as 07911 123456 is 447911123456 internationally. Keep that 0 after the country code and you get 4407911123456, which points to nobody.

A quick sanity check: E.164 numbers have at most 15 digits, and anything under 7 is almost certainly incomplete.

Rule 2: encode the message with encodeURIComponent

The prefilled message lives in a query string, so every character with a special meaning in a URL has to be escaped. The classic mistake is reaching for encodeURI, which deliberately leaves those characters alone because it assumes you're encoding a whole URL.

Here's what that does to three real messages:

Message encodeURI (wrong) encodeURIComponent (right)
Tom & Jerry tickets Tom%20&%20Jerry%20tickets Tom%20%26%20Jerry%20tickets
Status of order #1042? Status%20of%20order%20#1042? Status%20of%20order%20%231042%3F
$50 + tax $50%20+%20tax %2450%20%2B%20tax

With encodeURI, the & starts a new query parameter, so the message is cut to "Tom". The # starts a URL fragment, so everything after "order" never reaches WhatsApp. And a raw + is commonly decoded as a space. Line breaks need encoding too: \n becomes %0A.

A small helper

const digits = (s) => String(s).replace(/\D+/g, ''); function waLink(countryCode, phone, message = '') { const cc = digits(countryCode).replace(/^00/, ''); const number = (cc + digits(phone)).replace(/^0+/, ''); if (number.length < 7 || number.length > 15) { throw new Error('Not a valid international number'); } const text = message.trim(); return text ? `https://wa.me/${number}?text=${encodeURIComponent(text)}` : `https://wa.me/${number}`; } waLink('+1', '(202) 555-0123', 'Hi! Is the 2-bed still available?'); // https://wa.me/12025550123?text=Hi!%20Is%20the%202-bed%20still%20available%3F 

Note that the helper doesn't remove a trunk 0 that comes after the country code. That rule varies by country, so ask users for the number without it, and say so in the input label.

Common mistakes, in one list

  • Keeping the + or 00 in the path.
  • Leaving the domestic trunk 0 after the country code.
  • Encoding the message with encodeURI, or not encoding it at all.
  • Double-encoding: passing an already encoded string through encodeURIComponent turns %20 into %2520.
  • Testing only on desktop. Open the link on a phone that has WhatsApp installed before you ship it.

I wrote a longer, non-technical guide on how to create a WhatsApp link with wa.me, covering where to place it and when a QR code works better. If you just need a link for a bio or an email signature, the free WhatsApp link generator cleans the number and encodes the message for you, right in the browser.