> ## 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.

# Policy quickstart

> Create an API-only user, allow it to sign Ethereum transactions only to one address, verify the policy, and clean up, using the TypeScript server SDK or plain HTTP.

In this guide you add a second, non-root user to your organization and give it exactly one permission: sign Ethereum transactions to one allowlisted address. Everything runs from a script with released packages.

**Before you start:**

* [Account setup](https://docs.0xkey.io/getting-started/quickstart.md): your organization ID and a **root** API keypair.
* [Create a wallet](https://docs.0xkey.io/getting-started/create-wallet.md): a wallet with an Ethereum account.

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
npm install @0xkey-io/sdk-server @0xkey-io/crypto viem
```

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { ZeroXKey } from "@0xkey-io/sdk-server";

const root = new ZeroXKey({
  apiBaseUrl: "https://api.0xkey.io",
  apiPublicKey: process.env.ZEROXKEY_API_PUBLIC_KEY!,
  apiPrivateKey: process.env.ZEROXKEY_API_PRIVATE_KEY!,
  defaultOrganizationId: process.env.ZEROXKEY_ORG_ID!,
}).apiClient();
```

**1. Create an API-only user**

Generate a keypair for the new user and register its public key. The user starts with no permissions.

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

const policyTestKey = generateP256KeyPair(); // store privateKey in your secrets manager

const { userIds } = await root.createUsers({
  users: [
    {
      userName: "Policy Test",
      apiKeys: [
        {
          apiKeyName: "policy-test",
          publicKey: policyTestKey.publicKey,
          curveType: "API_KEY_CURVE_P256",
        },
      ],
      authenticators: [],
      oauthProviders: [],
      userTags: [],
    },
  ],
});
const userId = userIds[0];
```

**2. Create the policy**

Allow only this user, and only transactions whose recipient is `<ALLOWED_ADDRESS>` (lowercase).

```ts TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const { policyId } = await root.createPolicy({
  policyName: "Policy Test can pay one address",
  effect: "EFFECT_ALLOW",
  consensus: `approvers.any(user, user.id == '${userId}')`,
  condition: "eth.tx.to == '<ALLOWED_ADDRESS>'",
  notes: "Policy quickstart",
});
```

```json Policy JSON theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "effect": "EFFECT_ALLOW",
  "consensus": "approvers.any(user, user.id == '<USER_ID>')",
  "condition": "eth.tx.to == '<ALLOWED_ADDRESS>'"
}
```

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

**3. Test the policy**

Sign two transactions **as the new user**: one to the allowlisted address and one to any other address.

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { serializeTransaction, parseEther, parseGwei } from "viem";

const policyTest = new ZeroXKey({
  apiBaseUrl: "https://api.0xkey.io",
  apiPublicKey: policyTestKey.publicKey,
  apiPrivateKey: policyTestKey.privateKey,
  defaultOrganizationId: process.env.ZEROXKEY_ORG_ID!,
}).apiClient();

const tx = (to: `0x${string}`) =>
  serializeTransaction({
    type: "eip1559", chainId: 11155111, nonce: 0, to,
    value: parseEther("0.001"), gas: 21000n,
    maxFeePerGas: parseGwei("30"), maxPriorityFeePerGas: parseGwei("1"),
  });

for (const to of ["<ALLOWED_ADDRESS>", "<OTHER_ADDRESS>"] as const) {
  const res = await policyTest.signTransaction({
    signWith: "<YOUR_ETH_ADDRESS>",
    unsignedTransaction: tx(to),
    type: "TRANSACTION_TYPE_ETHEREUM",
  });
  console.log(to, res.activity.status);
}
```

The transaction to the allowlisted address is `ACTIVITY_STATUS_COMPLETED`. The other one is `ACTIVITY_STATUS_FAILED`, because no policy allows it. You've set your first policy.

**4. Clean up**

Delete the policy with the root key. `deletePolicies` takes a list of IDs; `deletePolicy` takes one.

```ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
await root.deletePolicies({ policyIds: [policyId] });
```

A policy ID that does not exist in the organization returns HTTP 404. Use `getPolicies` to list the current IDs.

## Using plain HTTP

Every call above is a stamped `POST`. For example, the policy in step 2 as a raw request:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -s https://api.0xkey.io/public/v1/submit/create_policy \
  -H "Content-Type: application/json" \
  -H "X-Stamp: $STAMP" \
  -d "$BODY"
```

where `$BODY` is:

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "type": "ACTIVITY_TYPE_CREATE_POLICY_V3",
  "timestampMs": "<milliseconds since epoch>",
  "organizationId": "<your organization id>",
  "parameters": {
    "policyName": "Policy Test can pay one address",
    "effect": "EFFECT_ALLOW",
    "consensus": "approvers.any(user, user.id == '<USER_ID>')",
    "condition": "eth.tx.to == '<ALLOWED_ADDRESS>'",
    "notes": "Policy quickstart"
  }
}
```

and `$STAMP` is your root API key's signature over the exact bytes of `$BODY`; see [Stamps](https://docs.0xkey.io/developer-reference/api-overview/stamps.md). The other steps use `create_users` (`ACTIVITY_TYPE_CREATE_USERS_V3`), `sign_transaction` (`ACTIVITY_TYPE_SIGN_TRANSACTION_V2`) and `delete_policies` (`ACTIVITY_TYPE_DELETE_POLICIES`).

## Next

* [Policy examples](https://docs.0xkey.io/policies/examples/access-control.md)
* [Policy overview](https://docs.0xkey.io/policies/overview.md)
* [Policy language](https://docs.0xkey.io/policies/language.md)


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