> ## Documentation Index
> Fetch the complete documentation index at: https://docs.0xkey.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Add a policy

> Give a non-root API user permission to sign transactions only to an allowlisted address, then verify the policy.

04 / QUICKSTART

Root users bypass policies. Everyone else can do **nothing** until a policy allows it. In this step you give a backend user exactly one permission: sign Ethereum transactions to one address.

**Before you start:** [sign a transaction](https://docs.0xkey.io/getting-started/sign-transaction.md) with your root API key.

## 1. Create a non-root user

Generate a keypair for a backend user and register its public key with `create_users`. The user starts with no permissions.

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
// npm install @0xkey-io/crypto
import { generateP256KeyPair } from "@0xkey-io/crypto";

const backendKey = generateP256KeyPair(); // store privateKey in your secrets manager
const { userIds } = await api.createUsers({
  users: [{
    userName: "Backend",
    apiKeys: [{ apiKeyName: "backend", publicKey: backendKey.publicKey, curveType: "API_KEY_CURVE_P256" }],
    authenticators: [],
    oauthProviders: [],
    userTags: [],
  }],
});
const userId = userIds[0]; // use as <USER_ID> below
```

## 2. Create the policy

Run this with your **root** credentials. Replace `<USER_ID>` and `<ALLOWED_ADDRESS>` (lowercase).

```ts TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const { policyId } = await api.createPolicy({
  policyName: "Backend can pay one address",
  effect: "EFFECT_ALLOW",
  consensus: "approvers.any(user, user.id == '<USER_ID>')",
  condition: "eth.tx.to == '<ALLOWED_ADDRESS>'",
  notes: "Getting started example",
});
```

```swift Swift theme={"theme":{"light":"github-light","dark":"github-dark"}}
guard let session = ctx.session, let client = ctx.client else { throw ZeroXKeyError.noActiveSession }

let res = try await client.createPolicy(TCreatePolicyBody(
    policyName: "Backend can pay one address",
    effect: .EFFECT_ALLOW,
    notes: "Getting started example",
    condition: "eth.tx.to == '<ALLOWED_ADDRESS>'",
    consensus: "approvers.any(user, user.id == '<USER_ID>')",
    organizationId: session.organizationId))
let policyId = res.policyId
```

```kotlin Kotlin theme={"theme":{"light":"github-light","dark":"github-dark"}}
import io.zeroxkey.types.TCreatePolicyBody
import io.zeroxkey.types.V1Effect

val orgId = ZeroXKeyContext.session.value!!.organizationId

val res = ZeroXKeyContext.client.createPolicy(TCreatePolicyBody(
    organizationId = orgId,
    policyName = "Backend can pay one address",
    effect = V1Effect.EFFECT_ALLOW,
    consensus = "approvers.any(user, user.id == '<USER_ID>')",
    condition = "eth.tx.to == '<ALLOWED_ADDRESS>'",
    notes = "Getting started example",
))
val policyId = res.result.policyId
```

```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
BODY='{
  "type": "ACTIVITY_TYPE_CREATE_POLICY_V3",
  "timestampMs": "'"$(date +%s)000"'",
  "organizationId": "'"$ZEROXKEY_ORG_ID"'",
  "parameters": {
    "policyName": "Backend can pay one address",
    "effect": "EFFECT_ALLOW",
    "consensus": "approvers.any(user, user.id == '"'"'<USER_ID>'"'"')",
    "condition": "eth.tx.to == '"'"'<ALLOWED_ADDRESS>'"'"'",
    "notes": "Getting started example"
  }
}'

curl -s https://api.0xkey.io/public/v1/submit/create_policy \
  -H "Content-Type: application/json" \
  -H "X-Stamp: $STAMP" \
  -d "$BODY"
```

> [!NOTE]
>
> `condition` is required: a missing, empty or whitespace-only condition is rejected with HTTP 400. Use `"true"` to match every activity.

> [!NOTE]
>
> **On mobile.** In an embedded wallet the signed-in end user is normally the root user of their own sub-organization, so policies do not restrict them. Policies in a sub-organization matter for other users you add there, for example a backend key used for [delegated access](https://docs.0xkey.io/policies/delegated-access/overview.md).

## 3. Test it

Sign two transactions with the **new user's** API key, as in the [previous step](https://docs.0xkey.io/getting-started/sign-transaction.md): one to `<ALLOWED_ADDRESS>` and one to any other address.

* To the allowlisted address: the activity is `ACTIVITY_STATUS_COMPLETED`.
* To any other address: the activity is `ACTIVITY_STATUS_FAILED`, because no policy allows it.

## 4. Clean up

Delete the policy with your root credentials, for example `api.deletePolicies({ policyIds: [policyId] })`. A policy ID that does not exist returns HTTP 404. See [Managing policies](https://docs.0xkey.io/policies/overview.md#managing-policies).

## Where to go next

> [card] [Policy language](https://docs.0xkey.io/policies/language.md)
>
> Conditions, consensus and transaction fields

> [card] [Policy examples](https://docs.0xkey.io/policies/examples/access-control.md)
>
> Ready-made patterns for access and signing control

> [card] [Embedded Wallets](https://docs.0xkey.io/embedded-wallets/overview.md)
>
> Wallets for your end users, one sub-organization each

> [card] [Company Wallets](https://docs.0xkey.io/company-wallets/overview.md)
>
> Automated signing for your own operations


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.