0

Submit Billing Data

by
Published Apr 8, 2025

Sends the billing and usage data. The partner should do this at least once a day and ideally once per hour. Use the `credentials.access_token` we provided in the [Upsert Installation](#upsert-installation) body to authorize this request.

Script vercel Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Vercel = {
3
  token: string;
4
};
5
/**
6
 * Submit Billing Data
7
 * Sends the billing and usage data. The partner should do this at least once a day and ideally once per hour.  Use the `credentials.access_token` we provided in the [Upsert Installation](#upsert-installation) body to authorize this request.
8
 */
9
export async function main(
10
  auth: Vercel,
11
  integrationConfigurationId: string,
12
  body: {
13
    timestamp: string;
14
    eod: string;
15
    period: { start: string; end: string };
16
    billing:
17
      | {
18
          billingPlanId: string;
19
          resourceId?: string;
20
          start?: string;
21
          end?: string;
22
          name: string;
23
          details?: string;
24
          price: string;
25
          quantity: number;
26
          units: string;
27
          total: string;
28
        }[]
29
      | {
30
          items: {
31
            billingPlanId: string;
32
            resourceId?: string;
33
            start?: string;
34
            end?: string;
35
            name: string;
36
            details?: string;
37
            price: string;
38
            quantity: number;
39
            units: string;
40
            total: string;
41
          }[];
42
          discounts?: {
43
            billingPlanId: string;
44
            resourceId?: string;
45
            start?: string;
46
            end?: string;
47
            name: string;
48
            details?: string;
49
            amount: string;
50
          }[];
51
        };
52
    usage: {
53
      resourceId?: string;
54
      name: string;
55
      type: "total" | "interval" | "rate";
56
      units: string;
57
      dayValue: number;
58
      periodValue: number;
59
      planValue?: number;
60
    }[];
61
  },
62
) {
63
  const url = new URL(
64
    `https://api.vercel.com/v1/installations/${integrationConfigurationId}/billing`,
65
  );
66

67
  const response = await fetch(url, {
68
    method: "POST",
69
    headers: {
70
      "Content-Type": "application/json",
71
      Authorization: "Bearer " + auth.token,
72
    },
73
    body: JSON.stringify(body),
74
  });
75
  if (!response.ok) {
76
    const text = await response.text();
77
    throw new Error(`${response.status} ${text}`);
78
  }
79
  return await response.text();
80
}
81