---
title: Resource scopes
description: Grant a key scopes for one project, site, or team instead of all of them.
---

Global scopes apply everywhere. Resource scopes apply to one resource, named `type:id`, such as `project:123` or `website:site_abc`. Use them when a key should work on some of an account's projects but not others.

## Grant them

```ts
const { key, record } = await keys.create({
  ownerId: 'user_123',
  scopes: ['projects:read'],
  resources: {
    'project:123': ['projects:write'],
    'project:456': ['projects:write', 'deploy'],
  },
})
```

This key can read every project, write to projects 123 and 456, and deploy only project 456.

To build `resources` in code, [`ResourceBuilder`](/docs/utilities/resource-builder) has `add`, `addMany` (the same scopes for several ids), and `remove`, and merges scopes added twice for one resource:

```ts
import { ResourceBuilder } from 'keypal'

const resources = new ResourceBuilder()
  .addMany('project', ['123', '456'], ['projects:write'])
  .add('project', '456', ['deploy'])
  .build()
```

## Check them

Pass the resource type and id from the request:

```ts
const projectId = '456'

keys.checkResourceScope(record, 'project', projectId, 'deploy')
keys.checkResourceAnyScope(record, 'project', projectId, ['deploy', 'admin'])
keys.checkResourceAllScopes(record, 'project', projectId, ['projects:read', 'deploy'])
```

A resource check grants a scope if the key holds it globally, holds it for that resource, or gets it through [`impliedScopes`](/docs/concepts/scopes-and-permissions). In the last line above, `projects:read` comes from the global scopes and `deploy` from `project:456`, so it returns `true`. These methods return `false` for a `null` record, so you can pass the result of `verifyFromHeaders` directly.

Each is shorthand for the matching scope check with a `resource` option: `keys.checkResourceScope(record, 'project', '456', 'deploy')` is `keys.hasScope(record, 'deploy', { resource: 'project:456' })`. A plain `keys.hasScope(record, 'deploy')` without `resource` only looks at global scopes.

## Global or per resource

Because global scopes count for every resource, a scope you mean to limit must not also be in `scopes`. A key with `scopes: ['deploy']` can deploy every project, whatever `resources` says.

A practical split: put read-only scopes that are safe everywhere in `scopes`, and anything that changes or deletes data in `resources`.

Resource ids are stored on the key, so a key does not gain access to projects created after it. Issue a new key, or rotate this one with new `resources`, to add them.
