# Public key authentication

How pk_ keys work, their security model, and how to configure them.

## What is a public key?

A public key is a **pk_…** string that identifies your business to the inFlow API. It's designed to be safely embedded in client-side code — it can submit forms and render content, but cannot access your dashboard, modify settings, or view other businesses' data.

Every business gets one public key. It is already filled into the snippet on your environment page: **Settings → Connections → Domains & environments → your environment → Script Tag**. See [Embedding scripts](/docs/embed-scripts).

## Origin enforcement

Public keys are bound to **environments** and/or **allowed origins**. When the inFlow API receives a request with a public key:

1. It looks up the business by `pk_…`
2. When Origin is present, it checks the business's environments, legacy allowedOrigins, and owned hosted sites
3. An unmatched supplied origin is rejected with **403 Forbidden**; lead and visitor-chat writes additionally reject missing or unvalidated Origin
4. Public read endpoints may accept a missing Origin. Each endpoint still has its own validation and scope requirements

> **Note:** **Localhost:** for forms and chat, `localhost` and `127.0.0.1` are accepted without registration only when the inFlow server runs outside production (`NODE_ENV !== "production"`, checked on the server, not on your site). On api.inflow.build this is never automatic: register `localhost` under **Domains & environments**. Analytics has no localhost exception at all, in any environment, so `localhost` must always be registered. The port is ignored.

## Hosted sites (automatic)

If your business has published sites hosted on inFlow's infrastructure (`mysite.inflow.build`), those domains are **auto-allowed**. You don't need to add them to an environment — the server detects that the requesting domain is one of your own hosted sites and passes the origin check automatically.

## Custom domains (environments)

For custom domains (e.g. `yoursite.com`), create an **environment** in your dashboard:

1. Go to **Settings → Connections → Domains & environments**
2. Create a new environment with your production domain
3. Link the relevant forms, showcases, and agents
4. Open the environment and copy the snippet from its **Script Tag** tab — it includes `data-tks-environment` for analytics attribution (see [Embedding scripts](/docs/embed-scripts))

The environment acts as both an origin allowlist and a content grouping — you can create separate environments for staging, production, and different subdomains.

## What public keys can't do

Public keys authorize designated visitor reads, form capture, and chat. They do not grant administrative access. They cannot:

1. Access the dashboard or admin panel
2. Create, update, or delete business settings
3. Manage users, permissions, or billing
4. View leads from other businesses
5. Access session-authenticated endpoints

Use the authenticated dashboard for administrative operations. Delegate supported website setup to an external agent with a temporary ik_ credential, not a dashboard session. Public keys belong in browser code; temporary integration secrets do not. See [API authentication](/docs/api-auth).

## Usage

```html
// In headers
Authorization: Bearer pk_67a1b2c3d4e5f6a7b8c9d0e1

// In script tags
<script
  src="https://api.inflow.build/toolkit.v2.js"
  data-tks-key="pk_67a1b2c3d4e5f6a7b8c9d0e1"
  data-tks-environment="your_environment_id"
  async
></script>

// In React components
<InflowForm publicKey="pk_67a1b2c3d4e5f6a7b8c9d0e1" ... />
<InflowChat publicKey="pk_67a1b2c3d4e5f6a7b8c9d0e1" ... />
<InflowShowcase publicKey="pk_67a1b2c3d4e5f6a7b8c9d0e1" ... />
```

## Rotating keys

To rotate a public key, contact support. Key rotation invalidates all existing embeds — you'll need to update every page that uses the old key. Plan a maintenance window and update all embed code before the old key is retired.
