# Configuration (https://docs.fawran.copto.org/sdk/configuration)

Configuration [#configuration]

Configure the Fawran SDK with your API keys and environment settings.

Initialization [#initialization]

```ts
import { Fawran } from "@copto/fawran";

const fawran = new Fawran({
  // fawran_sk_test_… selects sandbox, fawran_sk_live_… selects live
  secretKey: process.env.FAWRAN_SECRET_KEY,
});
```

Configuration Options [#configuration-options]

| Option      | Type                      | Required | Description                                                                                                  |
| ----------- | ------------------------- | -------- | ------------------------------------------------------------------------------------------------------------ |
| `secretKey` | `string`                  | ✅        | Your API secret key. Scoped keys (`fawran_sk_test_…` / `fawran_sk_live_…`) also select the environment       |
| `mode`      | `"sandbox" \| "live"`     | -        | Environment. Required for legacy (unscoped) keys; must agree with a scoped key                               |
| `apiUrl`    | `string`                  | -        | Custom API URL for self-hosted deployments (stands in for `mode`)                                            |
| `fetch`     | `typeof globalThis.fetch` | -        | Custom `fetch` implementation (e.g., Cloudflare Workers Service Binding `env.FAWRAN.fetch.bind(env.FAWRAN)`) |
| `debug`     | `boolean`                 | -        | Enable debug logging                                                                                         |
| `catalog`   | `CatalogEntry[]`          | -        | Declarative plan/feature definitions                                                                         |

Environments [#environments]

Fawran has two fully isolated environments, each on its own host:

| Environment | Host                               | Key prefix        |
| ----------- | ---------------------------------- | ----------------- |
| **sandbox** | `https://sandbox.fawran.copto.org` | `fawran_sk_test_` |
| **live**    | `https://api.fawran.copto.org`     | `fawran_sk_live_` |

A key only works against its own environment — a sandbox key sent to the live API is rejected with `401 environment_mismatch` before anything is written. Every response also echoes where it landed: the `X-Fawran-Environment` header (`sandbox` or `live`) plus `X-Fawran-Organization`, and `check()`/`track()` results include `environment` in the body.

The SDK never defaults to live [#the-sdk-never-defaults-to-live]

The environment is resolved from, in order:

1. `apiUrl` — explicit host (self-hosted / local)
2. `mode` — explicit `"sandbox"` or `"live"`
3. the key prefix — `fawran_sk_test_…` → sandbox, `fawran_sk_live_…` → live

If none of these determine it (a legacy `fawran_sk_…` key with no `mode`), the client still constructs, but the **first request throws** an `FawranError` with code `config_error` instead of silently talking to production. If `mode` contradicts the key's scope, that is also a `config_error`.

```ts
// Scoped key: nothing else needed
const fawran = new Fawran({ secretKey: process.env.FAWRAN_SECRET_KEY });
fawran.mode; // "sandbox" | "live", inferred from the key
fawran.apiUrl; // "https://sandbox.fawran.copto.org/v1"

// Legacy key: say which environment you mean
const legacy = new Fawran({
  secretKey: process.env.FAWRAN_SECRET_KEY, // fawran_sk_…
  mode: "sandbox",
});
```

Use the [dashboard](https://app.fawran.copto.org) to create one key per environment; legacy keys keep working on both hosts but should be rotated.

Custom API URL [#custom-api-url]

For self-hosted deployments or custom endpoints, use `apiUrl`. This takes precedence over `mode`:

```ts
const fawran = new Fawran({
  secretKey: process.env.FAWRAN_SECRET_KEY,
  apiUrl: "https://billing.mycompany.com",
  // mode is ignored when apiUrl is provided
});
```

Cloudflare Workers Service Bindings [#cloudflare-workers-service-bindings]

When your application runs on Cloudflare Workers in the same account as Fawran, pass a Service Binding's `fetch` to route requests directly over the Cloudflare isolate bus with zero external network latency:

```ts
const fawran = new Fawran({
  secretKey: env.FAWRAN_SECRET_KEY,
  fetch: env.FAWRAN.fetch.bind(env.FAWRAN),
});
```

URL Resolution Priority [#url-resolution-priority]

The SDK resolves the API URL in this order:

1. **Explicit `apiUrl`** (highest priority)
2. **`mode`** → `https://sandbox.fawran.copto.org/v1` or `https://api.fawran.copto.org/v1`
3. **Key prefix** → same hosts, inferred from `fawran_sk_test_` / `fawran_sk_live_`
4. Otherwise: **unresolved** — requests throw `config_error` (there is no default host)

Debug Mode [#debug-mode]

Enable debug mode for verbose logging:

```ts
const fawran = new Fawran({
  secretKey: process.env.FAWRAN_SECRET_KEY,
  mode: "sandbox",
  debug: true,
});
```

With Catalog [#with-catalog]

Pass a declarative catalog for plan/feature management:

```ts
import { metered, boolean, plan } from "@copto/fawran";

const fawran = new Fawran({
  secretKey: process.env.FAWRAN_SECRET_KEY,
  mode: "live",
  catalog: [
    plan("pro", {
      name: "Pro",
      price: 2900,
      currency: "USD",
      interval: "monthly",
      features: [
        metered("api-calls").limit(10000),
        boolean("premium-support").enabled(),
      ],
    }),
  ],
});

// Sync catalog to server
await fawran.sync();
```

Runtime Configuration [#runtime-configuration]

Override configuration at runtime (useful for CLI tooling):

```ts
const fawran = new Fawran({
  secretKey: process.env.FAWRAN_SECRET_KEY,
});

// Point at sandbox (both calls re-resolve fawran.mode / fawran.apiUrl)
fawran.setSecretKey(process.env.FAWRAN_SANDBOX_SECRET_KEY);
fawran.setApiUrl("https://sandbox.fawran.copto.org/v1");
```

Best Practices [#best-practices]

1. **Use environment variables** - Never hardcode API keys
2. **One scoped key per environment** - `fawran_sk_test_…` for sandbox, `fawran_sk_live_…` for live; the API refuses the wrong host
3. **Check the echo** - `X-Fawran-Environment` / `result.environment` tell you where a request actually landed
4. **Custom URL stands in for mode** - With `apiUrl`, `mode` is optional but must still agree with a scoped key

Examples [#examples]

Development Setup [#development-setup]

```ts
// config.ts
export const fawran = new Fawran({
  secretKey: process.env.FAWRAN_SANDBOX_SECRET_KEY!,
  mode: "sandbox",
  debug: process.env.NODE_ENV === "development",
});
```

Production Setup [#production-setup]

```ts
// config.ts
export const fawran = new Fawran({
  secretKey: process.env.FAWRAN_LIVE_SECRET_KEY!,
  mode: "live",
});
```

Dynamic Mode [#dynamic-mode]

```ts
// config.ts
const mode = process.env.FAWRAN_MODE as "sandbox" | "live" | undefined;
const secretKey =
  mode === "sandbox"
    ? process.env.FAWRAN_SANDBOX_SECRET_KEY
    : process.env.FAWRAN_LIVE_SECRET_KEY;

export const fawran = new Fawran({
  secretKey: secretKey!,
  mode, // undefined is fine with scoped keys; a legacy key needs it set
});
```

Related [#related]

* [Catalog Sync](/sdk/catalog) - Define plans and features declaratively
* [Quickstart](/getting-started/quickstart) - Get started with Fawran