0

Get Media URL

by
Published today

Retrieve the download URL and metadata of an uploaded or received media item. The URL is only valid for 5 minutes and requires the access token to download.

Script whatsapp_business Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
/**
4
 * Get Media URL
5
 * Retrieve the download URL and metadata of an uploaded or received media item. The URL is only valid for 5 minutes and requires the access token to download.
6
 */
7
export async function main(auth: RT.WhatsappBusiness, media_id: string) {
8
  const apiVersion = auth.api_version || "v25.0"
9
  const url = new URL(`https://graph.facebook.com/${apiVersion}/${media_id}`)
10
  url.searchParams.append("phone_number_id", auth.phone_number_id)
11

12
  const response = await fetch(url, {
13
    method: "GET",
14
    headers: {
15
      Authorization: `Bearer ${auth.token}`,
16
      Accept: "application/json",
17
    },
18
  })
19

20
  if (!response.ok) {
21
    throw new Error(`${response.status} ${await response.text()}`)
22
  }
23

24
  return await response.json()
25
}
26