Skip to main content

UC-017 — Download Media from Inbound Messages

Goal
Receive inbound messages with media and download the attached files
Channel
WhatsApp, RCS
Estimated time
15 minutes

Real-world scenarios​

  • AssicuraFacile — Accident photos: The customer sends photos of their car damage via WhatsApp. The operator downloads them to attach to the insurance claim.
  • StudioLegale Rossi — Documents via WhatsApp: A client sends a PDF contract for a quick review. The firm downloads it and archives it in the document management system.
  • RadioTaxi Milano — Voice messages: The driver sends a voice message with directions to the pickup point. The system downloads it and transcribes it automatically.

Media download flow​

The diagram illustrates the complete flow: the user sends a media file, the platform stores it and notifies your server via webhook with its mediaKey, and your application downloads the file through the dedicated endpoint.

Prerequisites​

  • Active API Key with message read permissions
  • Webhook endpoint configured for INBOUND events (see Webhook guide)
  • WhatsApp Business number or RCS agent registered on the platform
  • Local or cloud storage (S3, Azure Blob) for file saving

Step 1 — Configure the webhook for inbound messages​

Make sure your webhook endpoint is registered to receive INBOUND type events. When a user sends a message with an attachment, you will receive a notification like this:

{
"channel": "WHATSAPP",
"eventType": "INBOUND",
"messageId": "wamid.HBgNMzkzNDcxMjM0NTY3FQIAEhggQTJDNEU2RjgtMTIzNC01Njc4",
"source": "+393471234567",
"destination": "+393801234567",
"receivedDate": "2026-04-09T14:22:10Z",
"messageType": "IMAGE",
"mediaKey": "b394ab72/efd4987c/39b4879f6fed01f0d622453be1488c93"
}

The payload is flat, like every INBOUND event (see UC-015): source is the user's number, destination your WhatsApp number (your agent id on RCS), and mediaKey the key of the file. Null fields are left out.

Message types

messageType says what the user sent: TEXT for a text message, a media type such as IMAGE, AUDIO or VIDEO for a file, which then carries the mediaKey. The webhook gives neither the MIME type nor the size of the file: read them from the download.

Step 2 — Download the media file via mediaKey​

Use the mediaKey received in the webhook to obtain the file:

curl -X GET "https://api.qlara.ai/api/files/b394ab72/efd4987c/39b4879f6fed01f0d622453be1488c93" \
-H "X-Api-Key: YOUR_API_KEY"

Response — Download URL​

{
"key": "b394ab72/efd4987c/39b4879f6fed01f0d622453be1488c93",
"url": "https://fs-lora-namespace-public.fsn1.your-objectstorage.com/b394ab72/efd4987c/39b4879f6fed01f0d622453be1488c93?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=3600&X-Amz-Signature=..."
}

The response holds the storage key and a pre-signed url; it gives no MIME type, size or expiry date.

URL with expiration

The pre-signed URL is valid for 60 minutes by default. Set another validity with expireMinutes (1 to 360; values outside the range are brought back into it), for example GET /api/files/{mediaKey}?expireMinutes=180. Download the file before the URL expires; if it has expired, repeat the GET /api/files/{mediaKey} call to get a new one.

Step 3 — Download and save the file​

Use the pre-signed URL to download the actual file:

curl -o "sinistro_paraurti.jpg" \
"https://fs-lora-namespace-public.fsn1.your-objectstorage.com/b394ab72/efd4987c/39b4879f6fed01f0d622453be1488c93?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Expires=3600&X-Amz-Signature=..."
Behind the scenes — Media lifecycle
  1. Reception: When the user sends a file, the carrier (Meta for WhatsApp, Google for RCS) delivers the message to the Qlara platform, which stores the file in its own object storage.
  2. Notification: The INBOUND webhook carries the mediaKey, the key of the file in Qlara's storage: a path such as b394ab72/efd4987c/39b4879f6fed01f0d622453be1488c93.
  3. Pre-signed URL: The GET /api/files/{mediaKey} call does not return the file itself but a pre-signed URL pointing to it in object storage, valid for expireMinutes (60 by default). This avoids overloading the API with large file transfers.
  4. Availability: A key that is not in Qlara's storage is answered 404. Download and archive the files you need as soon as the webhook arrives.
  5. Size limits: The carrier sets the maximum size of each media type: see the WhatsApp media reference linked at the end of this page.

Step 4 — Process and archive​

Once downloaded, the file can be processed according to your needs:

# Example: save to a directory organized by date and case
PRATICA_ID="SIN-2026-04-0892"
DATA=$(date +%Y%m%d)
DEST_DIR="/archivio/sinistri/${PRATICA_ID}/${DATA}"

mkdir -p "$DEST_DIR"
mv sinistro_paraurti.jpg "${DEST_DIR}/foto_001.jpg"

echo "File archived in: ${DEST_DIR}/foto_001.jpg"

Expected result​

StepActionResult
1Webhook INBOUND receivedeventType: "INBOUND", messageType and mediaKey present
2GET /api/files/{mediaKey}Pre-signed URL (key, url)
3Download from pre-signed URLBinary file downloaded successfully
4ArchivingFile saved to business storage

Complete end-to-end example​

Here is the full AssicuraFacile scenario, from webhook reception to file saving:

#!/bin/bash
# Inbound media handling script for AssicuraFacile

MEDIA_KEY="b394ab72/efd4987c/39b4879f6fed01f0d622453be1488c93"
API_KEY="YOUR_API_KEY"
BASE_URL="https://api.qlara.ai"

# 1. Get the download URL
DOWNLOAD_URL=$(curl -s -X GET "${BASE_URL}/api/files/${MEDIA_KEY}" \
-H "X-Api-Key: ${API_KEY}" | jq -r '.url')

# 2. Download the file: type and size come from the download itself
MIME_TYPE=$(curl -s -o media_inbound -w '%{content_type}' "$DOWNLOAD_URL")
EXTENSION="${MIME_TYPE##*/}"
mv media_inbound "media_inbound.${EXTENSION}"
FILE_SIZE=$(wc -c < "media_inbound.${EXTENSION}")

echo "Type: ${MIME_TYPE}, Size: ${FILE_SIZE} bytes"
echo "File downloaded: media_inbound.${EXTENSION}"

# 3. Archive in the case management system
PRATICA_ID="SIN-2026-04-0892"
mv "media_inbound.${EXTENSION}" "/archivio/sinistri/${PRATICA_ID}/"

echo "Archived in case ${PRATICA_ID}"

Variants​

Handling messages with multiple attachments​

If the user sends multiple files, you will receive a webhook for each attachment. Iterate over the mediaKey values; keys are paths that contain /, so replace it when you build a file name:

MEDIA_KEYS=("b394ab72/efd4987c/39b4879f6fed01f0d622453be1488c93" "b394ab72/efd4987c/7d1e0c3a9b5f42e68c0a1f3b2d4e6a8c" "b394ab72/efd4987c/c2a4e6f8b0d24f6a8c0e2a4c6e8f0b2d")

for KEY in "${MEDIA_KEYS[@]}"; do
URL=$(curl -s -X GET "${BASE_URL}/api/files/${KEY}" \
-H "X-Api-Key: ${API_KEY}" | jq -r '.url')
curl -s -o "file_${KEY//\//_}" "$URL"
done

Common errors​

404 Not Found — Unknown mediaKey​

{
"status": "fail",
"data": "Media with key 'b394ab72/efd4987c/39b4879f6fed01f0d622453be1488c93' not found"
}

Solution: No file with that key is in Qlara's storage. Pass the mediaKey exactly as the webhook sent it, slashes included, and process the webhooks promptly.

401 Unauthorized — Missing API Key​

The endpoint answers 401 with an empty body.

Solution: Verify that the X-Api-Key header is present and that the key is active.

Expired pre-signed URL​

If the download fails with an HTTP 403 error, the pre-signed URL has expired. Request a new URL:

# The URL has expired, request a new one
curl -X GET "https://api.qlara.ai/api/files/b394ab72/efd4987c/39b4879f6fed01f0d622453be1488c93" \
-H "X-Api-Key: YOUR_API_KEY"

Next steps​

References​