When trying to download WhatsApp media (voice messages, images, videos) via the WhatsApp Business API, the response was an HTML file instead of the actual media.
Step 1: Get Media ID URL
First, I retrieved the media URL using the media ID:
curl -X GET "https://graph.facebook.com/v18.0/{media_id}?access_token={access_token}"
This returned a valid url
pointing to lookaside.fbsbx.com
.
Step 2: Download Media
I tried downloading the media file using:
curl -X GET "{media_url}" -H "Authorization: Bearer {access_token}" -o media_file
Issue: Instead of an audio file, I got an HTML document.
Step 3: Checking API Response
file media_file
and saw:
HTML document, Unicode text, UTF-8 text...
1. Assign Business Access to the App User
2. Generate a Long-Lived User Access Token (Not a Page Access Token!)
Key Learning:
WhatsApp Business Media API requires a long-lived user access token, not a page access token.
After fixing authentication, the correct way to download WhatsApp media:
curl -X GET "{media_url}" -H "Authorization: Bearer {long_lived_user_access_token}" -o voice_message.ogg
Then, check and play the file:
file voice_message.ogg # Verify file type
ffplay voice_message.ogg # Play audio (Linux/macOS)
Media successfully downloaded and working!