Download an artifact

Gets a redirect URL to download an archive for a repository. This URL expires after 1 minute. Look for `Location:` in the response header to find the URL for the download. The `:archive_format` must be `zip`. Anyone with read access to the repository can use this endpoint. If the repository is private you must use an access token with the `repo` scope. GitHub Apps must have the `actions:read` permission to use this endpoint.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 366 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Download an artifact
6
 * Gets a redirect URL to download an archive for a repository. This URL expires after 1 minute. Look for `Location:` in
7
the response header to find the URL for the download. The `:archive_format` must be `zip`. Anyone with read access to
8
the repository can use this endpoint. If the repository is private you must use an access token with the `repo` scope.
9
GitHub Apps must have the `actions:read` permission to use this endpoint.
10
 */
11
export async function main(
12
  auth: Github,
13
  owner: string,
14
  repo: string,
15
  artifact_id: string,
16
  archive_format: string
17
) {
18
  const url = new URL(
19
    `https://api.github.com/repos/${owner}/${repo}/actions/artifacts/${artifact_id}/${archive_format}`
20
  );
21

22
  const response = await fetch(url, {
23
    method: "GET",
24
    headers: {
25
      Authorization: "Bearer " + auth.token,
26
    },
27
    body: undefined,
28
  });
29
  if (!response.ok) {
30
    const text = await response.text();
31
    throw new Error(`${response.status} ${text}`);
32
  }
33
  return await response.text();
34
}
35