0

Download a file

by
Published Oct 17, 2025

Download a file by providing the file owner, access expiry, and a valid signature. Example cURL request: ```console curl -X GET "https://api.replicate.com/v1/files/cneqzikepnug6xezperrr4z55o/download?expiry=1708515345&owner=mattt&signature=zuoghqlrcnw8YHywkpaXQlHsVhWen%2FDZ4aal76dLiOo%3D" ```

Script replicate Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Replicate = {
3
  token: string;
4
};
5
/**
6
 * Download a file
7
 * Download a file by providing the file owner, access expiry, and a valid signature.
8

9
Example cURL request:
10

11
```console
12
curl -X GET "https://api.replicate.com/v1/files/cneqzikepnug6xezperrr4z55o/download?expiry=1708515345&owner=mattt&signature=zuoghqlrcnw8YHywkpaXQlHsVhWen%2FDZ4aal76dLiOo%3D"
13
```
14

15
 */
16
export async function main(
17
  auth: Replicate,
18
  file_id: string,
19
  owner: string | undefined,
20
  expiry: string | undefined,
21
  signature: string | undefined,
22
) {
23
  const url = new URL(`https://api.replicate.com/v1/files/${file_id}/download`);
24
  for (const [k, v] of [
25
    ["owner", owner],
26
    ["expiry", expiry],
27
    ["signature", signature],
28
  ]) {
29
    if (v !== undefined && v !== "" && k !== undefined) {
30
      url.searchParams.append(k, v);
31
    }
32
  }
33
  const response = await fetch(url, {
34
    method: "GET",
35
    headers: {
36
      Authorization: "Bearer " + auth.token,
37
    },
38
    body: undefined,
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.text();
45
}
46