# Standalone Functions

Standalone functions use the same organization API key as the SDK class. Call them only from trusted server code or serverless functions that keep the key secret. Never bundle the key for a browser.

Each SDK method is also available as a standalone function. Bundlers can remove unused operations from server deployments.

## Example

```typescript
import { LedgerCore } from "integrateledger/core.js";
import { createFeature } from "integrateledger/funcs/create-feature.js";

const ledger = new LedgerCore({ apiKey: process.env.LEDGER_API_KEY ?? "" });
const result = await createFeature(ledger, {
  id: "api_call",
  name: "API call",
  creditCost: 1,
});
if (!result.ok) throw result.error;
console.log(result.value.item.id);
```

## Result types

Standalone functions differ from SDK methods in that they return a
`Result<Value, Error>` type to capture _known errors_ and document them using
the type system. By avoiding throwing errors, application code maintains clear
control flow and error-handling become part of the regular flow of application
code.

> We use the term "known errors" because standalone functions, and JavaScript
> code in general, can still throw unexpected errors such as `TypeError`s,
> `RangeError`s and `DOMException`s. Exhaustively catching all errors may be
> something this SDK addresses in the future. Nevertheless, there is still a lot
> of benefit from capturing most errors and turning them into values.

Result values also make expected API errors explicit in server handlers, where each request can choose whether to retry, log, or return an error to the caller.

The `result.ok` check in the example above narrows the return type, so `result.error` and `result.value` have explicit types.
