0

Get payments

by
Published Apr 8, 2025

There are two methods to query for a user's payment information on a plugin, widget, or Community file. The first method, using plugin payment tokens, is typically used when making queries from a plugin's or widget's code. The second method, providing a user ID and resource ID, is typically used when making queries from anywhere else. Note that you can only query for resources that you own. In most cases, this means that you can only query resources that you originally created.

Script figma Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Figma = {
3
  token: string;
4
};
5
/**
6
 * Get payments
7
 * There are two methods to query for a user's payment information on a plugin, widget, or Community file. The first method, using plugin payment tokens, is typically used when making queries from a plugin's or widget's code. The second method, providing a user ID and resource ID, is typically used when making queries from anywhere else.
8

9
Note that you can only query for resources that you own. In most cases, this means that you can only query resources that you originally created.
10
 */
11
export async function main(
12
  auth: Figma,
13
  plugin_payment_token: string | undefined,
14
  user_id: string | undefined,
15
  community_file_id: string | undefined,
16
  plugin_id: string | undefined,
17
  widget_id: string | undefined,
18
) {
19
  const url = new URL(`https://api.figma.com/v1/payments`);
20
  for (const [k, v] of [
21
    ["plugin_payment_token", plugin_payment_token],
22
    ["user_id", user_id],
23
    ["community_file_id", community_file_id],
24
    ["plugin_id", plugin_id],
25
    ["widget_id", widget_id],
26
  ]) {
27
    if (v !== undefined && v !== "" && k !== undefined) {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      Authorization: "Bearer " + auth.token,
35
    },
36
    body: undefined,
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44