0

Query files/folders by metadata

by
Published Oct 17, 2025

Create a search using SQL-like syntax to return items that match specific metadata. By default, this endpoint returns only the most basic info about the items for which the query matches. To get additional fields for each item, including any of the metadata, use the `fields` attribute in the query.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * Query files/folders by metadata
7
 * Create a search using SQL-like syntax to return items that match specific
8
metadata.
9

10
By default, this endpoint returns only the most basic info about the items for
11
which the query matches. To get additional fields for each item, including any
12
of the metadata, use the `fields` attribute in the query.
13
 */
14
export async function main(
15
  auth: Box,
16
  body: {
17
    from: string;
18
    query?: string;
19
    query_params?: {};
20
    ancestor_folder_id: string;
21
    order_by?: {
22
      field_key?: string;
23
      direction?: "ASC" | "DESC" | "asc" | "desc";
24
    }[];
25
    limit?: number;
26
    marker?: string;
27
    fields?: string[];
28
  },
29
) {
30
  const url = new URL(`https://api.box.com/2.0/metadata_queries/execute_read`);
31

32
  const response = await fetch(url, {
33
    method: "POST",
34
    headers: {
35
      "Content-Type": "application/json",
36
      Authorization: "Bearer " + auth.token,
37
    },
38
    body: JSON.stringify(body),
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46