0

Create opening balance

by
Published Oct 17, 2025

Creates opening balance with the given information.

Script zoho Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Zoho = {
3
  token: string;
4
};
5
/**
6
 * Create opening balance
7
 * Creates opening balance with the given information.
8
 */
9
export async function main(
10
  auth: Zoho,
11
  organization_id: string | undefined,
12
  body: {
13
    date: string;
14
    accounts?: {
15
      account_id?: string;
16
      debit_or_credit: string;
17
      exchange_rate?: number;
18
      currency_id?: string;
19
      amount?: number;
20
      location_id?: string;
21
    }[];
22
  },
23
) {
24
  const url = new URL(
25
    `https://www.zohoapis.com/books/v3/settings/openingbalances`,
26
  );
27
  for (const [k, v] of [["organization_id", organization_id]]) {
28
    if (v !== undefined && v !== "" && k !== undefined) {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "POST",
34
    headers: {
35
      "Content-Type": "application/json",
36
      Authorization: "Zoho-oauthtoken " + 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