API Reference

REST API

HTTP endpoints for direct integration

React Hooks

React hooks for state management

TypeScript Types

Type definitions and interfaces

Authentication

All API requests require authentication using your API credentials.

1const headers = {
2 Authorization: "Bearer your-api-token",
3 "X-Company-ID": "your-company-id",
4 "Content-Type": "application/json",
5};

REST API Endpoints

Accounts

List Accounts

1GET /api/v1/accounts

Query Parameters:

  • type (string): Filter by account type
  • parent_id (string): Filter by parent account
  • page (number): Page number for pagination
  • limit (number): Items per page

Response:

1{
2 "data": [
3 {
4 "id": "acc_123",
5 "name": "Cash",
6 "type": "asset",
7 "balance": "1000.00",
8 "currency": "USD",
9 "parent_id": null,
10 "created_at": "2024-01-01T00:00:00Z"
11 }
12 ],
13 "meta": {
14 "total": 100,
15 "page": 1,
16 "limit": 25
17 }
18}

Create Account

1POST /api/v1/accounts

Request Body:

1{
2 "name": "New Account",
3 "type": "asset",
4 "currency": "USD",
5 "parent_id": "acc_parent"
6}

Transactions

List Transactions

1GET /api/v1/transactions

Query Parameters:

  • account_id (string): Filter by account
  • date_from (string): Start date
  • date_to (string): End date
  • type (string): Transaction type
  • status (string): Transaction status

Response:

1{
2 "data": [
3 {
4 "id": "txn_123",
5 "date": "2024-01-01",
6 "description": "Monthly Rent",
7 "amount": "2000.00",
8 "currency": "USD",
9 "status": "posted",
10 "entries": [
11 {
12 "account_id": "acc_123",
13 "amount": "2000.00",
14 "type": "debit"
15 },
16 {
17 "account_id": "acc_456",
18 "amount": "2000.00",
19 "type": "credit"
20 }
21 ]
22 }
23 ],
24 "meta": {
25 "total": 50,
26 "page": 1
27 }
28}

Create Transaction

1POST /api/v1/transactions

Request Body:

1{
2 "date": "2024-01-01",
3 "description": "Office Supplies",
4 "currency": "USD",
5 "entries": [
6 {
7 "account_id": "acc_123",
8 "amount": "100.00",
9 "type": "debit"
10 },
11 {
12 "account_id": "acc_456",
13 "amount": "100.00",
14 "type": "credit"
15 }
16 ]
17}

Reports

Generate Balance Sheet

1GET /api/v1/reports/balance-sheet

Query Parameters:

  • as_of (string): Balance sheet date
  • comparison_date (string): Optional comparison date
  • currency (string): Report currency

Generate Income Statement

1GET /api/v1/reports/income-statement

Query Parameters:

  • from_date (string): Start date
  • to_date (string): End date
  • comparison_period (string): Optional comparison period
  • currency (string): Report currency

React Hooks

useAccounts

1import { useAccounts } from "@openledger/accounting-react";
2
3function AccountList() {
4 const {
5 accounts,
6 isLoading,
7 error,
8 fetchAccounts,
9 createAccount,
10 updateAccount,
11 } = useAccounts({
12 type: "asset",
13 includeBalance: true,
14 });
15
16 // Use the accounts data and methods
17}

useTransactions

1import { useTransactions } from "@openledger/accounting-react";
2
3function TransactionList() {
4 const {
5 transactions,
6 isLoading,
7 error,
8 fetchTransactions,
9 createTransaction,
10 updateTransaction,
11 } = useTransactions({
12 accountId: "acc_123",
13 dateRange: {
14 start: "2024-01-01",
15 end: "2024-12-31",
16 },
17 });
18
19 // Use the transactions data and methods
20}

useReports

1import { useReports } from "@openledger/accounting-react";
2
3function FinancialReports() {
4 const {
5 balanceSheet,
6 incomeStatement,
7 isLoading,
8 error,
9 generateBalanceSheet,
10 generateIncomeStatement,
11 } = useReports({
12 asOf: "2024-12-31",
13 currency: "USD",
14 });
15
16 // Use the reports data and methods
17}

TypeScript Types

Account Types

1interface Account {
2 id: string;
3 name: string;
4 type: AccountType;
5 balance: string;
6 currency: string;
7 parent_id: string | null;
8 created_at: string;
9 updated_at: string;
10}
11
12type AccountType = "asset" | "liability" | "equity" | "revenue" | "expense";
13
14interface AccountCreateParams {
15 name: string;
16 type: AccountType;
17 currency: string;
18 parent_id?: string;
19}

Transaction Types

1interface Transaction {
2 id: string;
3 date: string;
4 description: string;
5 currency: string;
6 status: TransactionStatus;
7 entries: TransactionEntry[];
8 metadata?: Record<string, any>;
9 created_at: string;
10 updated_at: string;
11}
12
13interface TransactionEntry {
14 account_id: string;
15 amount: string;
16 type: "debit" | "credit";
17 description?: string;
18}
19
20type TransactionStatus = "draft" | "posted" | "pending" | "void";

Report Types

1interface BalanceSheet {
2 as_of: string;
3 currency: string;
4 assets: AccountBalance[];
5 liabilities: AccountBalance[];
6 equity: AccountBalance[];
7 total_assets: string;
8 total_liabilities: string;
9 total_equity: string;
10}
11
12interface IncomeStatement {
13 from_date: string;
14 to_date: string;
15 currency: string;
16 revenue: AccountBalance[];
17 expenses: AccountBalance[];
18 net_income: string;
19}
20
21interface AccountBalance {
22 account_id: string;
23 name: string;
24 balance: string;
25 children?: AccountBalance[];
26}

Error Handling

All API endpoints return standard HTTP status codes:

  • 200: Success
  • 400: Bad Request
  • 401: Unauthorized
  • 403: Forbidden
  • 404: Not Found
  • 422: Validation Error
  • 500: Server Error

Error Response Format:

1{
2 "error": {
3 "code": "validation_error",
4 "message": "Invalid account type",
5 "details": {
6 "field": "type",
7 "value": "invalid_type"
8 }
9 }
10}

Rate Limits

  • 1000 requests per minute per API key
  • 10,000 requests per day per API key
  • Webhook delivery: 10 concurrent connections

Rate limit headers:

1X-RateLimit-Limit: 1000
2X-RateLimit-Remaining: 999
3X-RateLimit-Reset: 1640995200

Webhooks

Configure webhooks to receive real-time updates:

1POST /api/v1/webhooks

Request Body:

1{
2 "url": "https://your-domain.com/webhook",
3 "events": ["transaction.created", "transaction.updated", "account.updated"],
4 "secret": "your-webhook-secret"
5}