# User

> Users: the people and companies your product acts for, each known by your own id.

## noclick.User

```python
class User
```

One of your users, a person or a company, known by your own id for it.
`User.from_id("user_42", create_if_missing=True)` is lazy: nothing is sent
until the user is used.

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `str` | Your id for the user. |
| `name` | `str | None` |  |
| `email` | `str | None` |  |
| `phone` | `str | None` |  |
| `phone_verified_at` | `Any` | When the phone was proven (None if never). |
| `spend_cap_usd_per_month` | `float | None` |  |
| `metadata` | `dict[str, Any] | None` |  |
| `timezone` | `str | None` | Their IANA timezone, like `Europe/Lisbon`: agents acting for them tell the time in it, and `Cron(tz="user")` fires in it. None: UTC. |
| `status` | `str` |  |
| `created_at` | `Any` |  |
| `spending` | `Spending | None` | Whether they can spend right now: paused or not, what NoClick tells them, and how many of their messages wait (see `pause_spending`). |
| `console_url` | `str | None` | This user in the developer console (None off the hosted API). |
| `human` | `Human` | The person behind this user: `user.human.approve("Send it?").wait()`. |

### User.create

```python
noclick.User.create(
    id, *, name=None, email=None, phone=None, spend_cap_usd_per_month=None,
    metadata=None, timezone=None, allow_existing=False, idempotency_key=None,
    client=None
) -> User
```

Create a user, keyed by your own `id` for it: 1 to 255 printable ASCII
characters, without spaces or `/`, other than `all` and `none`. An id
another live user has raises `noclick.Conflict` (its `existing_id` is
that id), or with `allow_existing=True` returns that user, as it is: its details
don't change. `timezone` is where they are, as an IANA
name (`"America/New_York"`): every turn of an agent acting for them starts
with the date and time there, unless the agent has a `timezone` of its own.

Parameters:

- `id` (`str`, required)
- `name` (`str | None`, default `None`)
- `email` (`str | None`, default `None`)
- `phone` (`str | None`, default `None`)
- `spend_cap_usd_per_month` (`float | None`, default `None`)
- `metadata` (`dict[str, Any] | None`, default `None`)
- `timezone` (`str | None`, default `None`)
- `allow_existing` (`bool`, default `False`)
- `idempotency_key` (`str | None`, default `None`): Makes a retried create safe: the same key returns the first result. Generated when omitted.
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `User` The new user, loaded.

Usage:

Create one when a user signs up, keyed by your own id for them:

```python
user = noclick.User.create("user_42", name="Ana Lima", email="ana@example.com")
print(user.id)   # user_42
```

If the user may already exist, take it as it is:

```python
user = noclick.User.create("user_42", allow_existing=True)
```

Say where they are, so their agents know what "tomorrow morning" means:

```python
user = noclick.User.create("user_42", email="ana@example.com", timezone="America/Sao_Paulo")
```

### User.from_id

```python
noclick.User.from_id(
    id, *, create_if_missing=False, name=..., email=..., phone=...,
    spend_cap_usd_per_month=..., metadata=..., timezone=..., client=None
) -> User
```

A lazy reference to the user with your `id`. Given fields, it gets the user and sets
the ones you pass, on every run: an existing user whose `name`, `email`, `phone`,
`spend_cap_usd_per_month`, `metadata` or `timezone` differ gets yours. A field you
leave out is never changed, and `None` clears one, as in `update()`.
`create_if_missing=True` creates it with the given fields when there's none; without
it, a missing user raises `noclick.NotFound`.

Parameters:

- `id` (`str`, required)
- `create_if_missing` (`bool`, default `False`)
- `name` (`Any`, default: unchanged)
- `email` (`Any`, default: unchanged)
- `phone` (`Any`, default: unchanged)
- `spend_cap_usd_per_month` (`Any`, default: unchanged)
- `metadata` (`Any`, default: unchanged)
- `timezone` (`Any`, default: unchanged)
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `User` A user that loads, or is created, the first time it's used. Nothing is sent yet.

Usage:

Refer to a user by your id without a request:

```python
user = noclick.User.from_id("user_42")
print(user.status)   # loads it now; noclick.NotFound if there's none
```

Or create it the first time it's used, so sign-up needs no extra call.
Two requests that race both get the same user:

```python
user = noclick.User.from_id("user_42", create_if_missing=True, email="ana@example.com")
with noclick.as_user(user):
    agent = noclick.Agent.from_name("assistant")
```

Run at every sign-in, it sets the user's details to yours:

```python
user = noclick.User.from_id("user_42", create_if_missing=True, name="Ana Lima", email="ana@example.com")
print(user.email)   # ana@example.com, even if it was another address before
```

Pass the timezone your product already knows:

```python
user = noclick.User.from_id("user_42", create_if_missing=True, timezone="Europe/Lisbon")
print(user.timezone)
```

### User.get

```python
noclick.User.get(id, *, client=None) -> User
```

Load a user by your id now; an unknown id raises `noclick.NotFound`.

Parameters:

- `id` (`str`, required)
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `User` The user, loaded.

Usage:

```python
user = noclick.User.get("user_42")
print(user.name, user.status)
```

### User.list

```python
noclick.User.list(
    *, q=None, status=None, spending=None, limit=None, client=None
) -> list[User]
```

Your users, newest first (or the first `limit`). `q` is a
case-insensitive prefix of the id, name or email; `status`
`active` or `suspended`; `spending` `"paused"` or `"active"`, to find the users
whose spending you've paused (say, to check them against your billing after an outage).

Parameters:

- `q` (`str | None`, default `None`)
- `status` (`str | None`, default `None`)
- `spending` (`str | None`, default `None`)
- `limit` (`int | None`, default `None`): Return at most this many.
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `list[User]` Every matching user, fetched page by page.

Usage:

```python
for user in noclick.User.list(q="ana"):
    print(user.id, user.email)

suspended = noclick.User.list(status="suspended", limit=20)
paused = noclick.User.list(spending="paused")
```

### User.list_page

```python
noclick.User.list_page(
    *, cursor=None, limit=None, q=None, status=None, spending=None, client=None
) -> Page
```

One page of users; pass `next_cursor` back as `cursor`. Takes `list`'s filters.

Parameters:

- `cursor` (`str | None`, default `None`): The `next_cursor` of the page before; omit it for the first page.
- `limit` (`int | None`, default `None`): Return at most this many.
- `q` (`str | None`, default `None`)
- `status` (`str | None`, default `None`)
- `spending` (`str | None`, default `None`)
- `client` (`Client | None`, default `None`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: `Page` A page whose `data` holds the users and whose `next_cursor` is `None` on the last page.

Usage:

```python
page = noclick.User.list_page(limit=50)
while True:
    for user in page.data:
        print(user.id)
    if page.next_cursor is None:
        break
    page = noclick.User.list_page(limit=50, cursor=page.next_cursor)
```

### User.update

```python
user.update(
    *, id=..., name=..., email=..., phone=..., spend_cap_usd_per_month=...,
    metadata=..., timezone=...
) -> User
```

Change fields (`None` clears one); a field you leave out is unchanged.
A new `id` renames the user: everything it owns follows, and an id another
user has raises `noclick.Conflict`. A user acting for themselves may change
their own `timezone`, their session included (it may change nothing else).

Parameters:

- `id` (`Any`, default: unchanged)
- `name` (`Any`, default: unchanged)
- `email` (`Any`, default: unchanged)
- `phone` (`Any`, default: unchanged)
- `spend_cap_usd_per_month` (`Any`, default: unchanged)
- `metadata` (`Any`, default: unchanged)
- `timezone` (`Any`, default: unchanged)

Returns: `User` This user, updated.

Usage:

Change a user's details:

```python
user = noclick.User.from_id("user_42")
user.update(name="Ana Lima-Souza", phone=None, spend_cap_usd_per_month=50)
```

Move a user to a new id, say when your own ids change:

```python
user = noclick.User.from_id("user_42")
user.update(id="acct_9f3a")
print(user.id)   # acct_9f3a
```

They moved: their agents' next turns and `Cron(tz="user")` schedules follow:

```python
user = noclick.User.from_id("user_42")
user.update(timezone="Asia/Tokyo")
```

### User.suspend

```python
user.suspend() -> User
```

Stop all activity for this user: every call made for it then fails
with `noclick.PermissionDenied`, and its history stays readable.

Returns: `User` This user, now `suspended`.

Usage:

```python
user = noclick.User.from_id("user_42")
user.suspend()
print(user.status)   # suspended
```

### User.pause_spending

```python
user.pause_spending(*, message=None, action=None) -> User
```

Stop everything that costs money for this user until `resume_spending()`, and
say what they can do about it. Call it from your billing when they run out (say, at a
balance alert) or fall behind. `message` is what NoClick tells them, in your words,
up to 500 characters; `action` is a `(label, url)` pair, your checkout or billing
page, up to 40 characters and an `https` URL. Both may use `{{user.id}}`,
`{{user.name}}`, `{{user.email}}` and `{{user.metadata.<key>}}`, filled in now;
a field this user lacks raises `noclick.InvalidRequest`. Without `message`, the
default you set in the console (Settings, Paused spending) is used, and with no default
either it raises `noclick.InvalidRequest`; without `action`, the default's button.

While paused, a call your code makes for them raises `noclick.SpendingPaused`; a
message they send an agent (an email to its inbox, a post in their channel) is answered
once per conversation with `message` and `action` and held, up to 24 hours, to run
when you resume; a call to a number they reach hears `message`; schedules, webhooks
and app events for their agents are skipped. Work under way finishes the step it's on.
What they already have keeps running and billing: pausing isn't cancelling. Reads
still work. Pausing a paused user changes its message and action and keeps `since`
and what's held. Only a key acting for your whole account can pause or resume: never
the user's own session.

Parameters:

- `message` (`str | None`, default `None`)
- `action` (`tuple[str, str] | None`, default `None`)

Returns: `User` This user, with `spending` paused.

Usage:

Pause a user when their balance runs low, with your top-up page:

```python
user = noclick.User.from_id("user_42")
user.pause_spending(
    message="You've used your credits.",
    action=("Top up", "https://acme.com/billing/top-up?user={{user.id}}"),
)
print(user.spending.paused, user.spending.since)
```

Or with the message you set in the console:

```python
noclick.User.from_id("user_42").pause_spending()
```

### User.resume_spending

```python
user.resume_spending() -> User
```

Let this user spend again, say once your billing has their payment. What was held
for them runs now, in the order it arrived: each message as the turn it would have
been, with when it arrived, so the agent knows it waited. Resuming a user who isn't
paused changes nothing.

Returns: `User` This user, with `spending` no longer paused.

Usage:

```python
user = noclick.User.from_id("user_42")
held = user.spending.held
user.resume_spending()
print(f"Resumed; {held} held messages are running now")
```

### User.delete

```python
user.delete(*, export=False) -> Operation | None
```

Mark the user deleted, which frees its id for reuse. Nothing acts for
them afterwards (invite-only beta): their connections' grants are
revoked at each provider, their agents' triggers stop, their inboxes stop
receiving mail and their phone numbers are released. What's kept is the
record: operations, tool calls, conversations, usage and spend stay readable
with `user_filter`, and their databases, buckets, repos and files stay until
you delete them. `export=True` packs up everything the user holds first,
as `export()` does, and deletes the user once the export is stored; nothing
is deleted if the export fails or is canceled. Only a call acting for the
account deletes users.

Parameters:

- `export` (`bool`, default `False`)

Returns: `Operation | None` `None`; with `export=True`, an operation whose `.wait()` gives the
`ExportBundle` once the user is deleted.

Usage:

Delete a user:

```python
noclick.User.from_id("user_42").delete()
```

Or hand the user their data, then delete them:

```python
user = noclick.User.from_id("user_42")
bundle = user.delete(export=True).wait()
print(bundle.url, bundle.expires_at)
```

### User.export

```python
user.export() -> Operation
```

Pack up everything the user holds, in forms that work without
NoClick: their own agents' definitions as JSON, their conversations with
any agent as JSON Lines transcripts, what agents remember about them as
Markdown, their databases as SQL, their buckets' and volumes' files,
their dicts and queues as JSON Lines, their repos as git bundles, their
apps' settings, connections and secrets by name only (never a token or a
value), and each phone number and domain with the call that moves it
(`transfer_out`). The user keeps working; one export of a user runs at
a time (another raises `noclick.Conflict` naming it).

Returns: `Operation` An operation whose `.wait()` gives the `ExportBundle`: a zip `url`
valid until `expires_at` (7 days), its `contents`, and
`remaining_dependencies`, what still runs on NoClick.

Usage:

```python
user = noclick.User.from_id("user_42")
bundle = user.export().wait()
print(bundle.url, bundle.expires_at)
for item in bundle.contents:
    print(item.kind, item.name, item.detail)
print("Still on NoClick:", ", ".join(bundle.remaining_dependencies))
```

### User.verify_phone

```python
user.verify_phone(*, phone=None) -> PhoneVerification
```

Text a verification code to the user's phone (or `phone=`, which
becomes its number); confirm it with `check_phone(code)`.

Parameters:

- `phone` (`str | None`, default `None`)

Returns: `PhoneVerification` The number the code went to, and when the code expires.

Usage:

```python
user = noclick.User.from_id("user_42")
sent = user.verify_phone(phone="+14155550100")
print(sent.phone, sent.expires_at)
```

### User.check_phone

```python
user.check_phone(code) -> bool
```

Check the code the user received. A wrong code raises
`InvalidRequest`, an expired one `Expired`, and too many tries
`RateLimited`.

Parameters:

- `code` (`str`, required)

Returns: `bool` `True` once the phone is verified; `phone_verified_at` is then set.

Usage:

```python
user = noclick.User.from_id("user_42")
try:
    user.check_phone("482913")
    print(user.phone_verified_at)
except noclick.InvalidRequest as err:
    print(err.message)   # That code isn't right.
```

### User.session

```python
user.session(scopes, *, ttl_s=None) -> Session
```

Mint a short-lived token that acts only as this user, with
`scopes` (a subset of your key's: `agents`, `humans`,
`credentials:request`, `credentials:use`, `channels`, `inbox`, `ai`).
`ttl_s` is at most 24h. Hand `session.token` to your frontend, or
use `noclick.Client.for_session(session)`. Revoking the minting key
revokes its sessions.

Parameters:

- `scopes` (`str | Sequence[str]`, required)
- `ttl_s` (`int | float | timedelta | None`, default `None`)

Returns: `Session` The session: its `token`, the `scopes` it carries and when it expires.

Usage:

Mint one per page load on your server and hand the token to the browser:

```python
user = noclick.User.from_id("user_42")
session = user.session(["agents", "humans"], ttl_s=3600)
token = session.token   # for the browser; it acts only as this user
```

Or act as the user from your own code, with only those scopes:

```python
client = noclick.Client.for_session(session)
agent = noclick.Agent.from_name("assistant", client=client)
```

## noclick.PhoneVerification

```python
class PhoneVerification
```

A verification code on its way to the user's phone.

| Attribute | Type | Description |
| --- | --- | --- |
| `phone` | `str | None` |  |
| `status` | `str | None` |  |
| `expires_at` | `Any` |  |
| `raw` | `dict[str, Any]` | The response JSON exactly as the API sent it. |

## noclick.Spending

```python
class Spending
```

Whether a user can spend right now, as `user.spending` reads it. A
`paused` user can't start anything that costs money until you call
`resume_spending()`; `message` and `action` (a `(label, url)` pair) are what
NoClick tells them meanwhile, `since` is when you paused them, `held` how many
of their messages wait to run when you resume, and `paused_by` the key that
paused them (`"console"` from the console).

| Attribute | Type | Description |
| --- | --- | --- |
| `paused` | `bool` |  |
| `message` | `str | None` |  |
| `action` | `tuple[str, str] | None` |  |
| `since` | `Any` |  |
| `held` | `int` |  |
| `paused_by` | `str | None` |  |
| `raw` | `dict[str, Any]` | The response JSON exactly as the API sent it. |
