> For the complete documentation index, see [llms.txt](https://roam-tui.avelino.run/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://roam-tui.avelino.run/sdk/client.md).

# Client

`RoamClient` is the async HTTP client for the Roam Research API.

## Creating a client

```rust
use roam_sdk::RoamClient;

let client = RoamClient::new("graph-name", "api-token");
```

The client is `Clone` — you can share it across async tasks.

## Methods

### `pull`

Fetch an entity by ID or lookup reference using a [pull expression](https://www.roamresearch.com/#/app/developer-documentation/page/eb8OVhaFC).

```rust
pub async fn pull(
    &self,
    eid: serde_json::Value,
    selector: &str,
) -> Result<PullResponse>
```

**Parameters:**

* `eid` — entity identifier. Either a direct eid or a lookup ref as an EDN string:
  * `json!("[:block/uid \"02-21-2026\"]")` — lookup by block UID
  * `json!("[:node/title \"My Page\"]")` — lookup by page title
* `selector` — EDN pull expression selecting which attributes to return

**Returns:** `PullResponse { result: serde_json::Value }`

**Example:**

```rust
use serde_json::json;

let eid = json!("[:node/title \"Projects\"]");
let selector = "[:block/uid :node/title :block/string {:block/children ...}]";

let resp = client.pull(eid, selector).await?;
let title = resp.result.get(":node/title").and_then(|v| v.as_str());
```

### `query`

Run a [Datalog query](https://www.roamresearch.com/#/app/developer-documentation/page/eb8OVhaFC) against the graph.

```rust
pub async fn query(
    &self,
    query: String,
    args: Vec<serde_json::Value>,
) -> Result<QueryResponse>
```

**Parameters:**

* `query` — Datalog query string with `:find` and `:where` clauses
* `args` — arguments for `:in` clause bindings (pass `vec![]` if none)

**Returns:** `QueryResponse { result: Vec<Vec<serde_json::Value>> }`

Each inner `Vec` is one result row, with values matching the `:find` variables.

**Example:**

```rust
let query = r#"[:find ?uid ?s
                :where [?b :block/string ?s]
                       [?b :block/uid ?uid]
                       [(clojure.string/includes? ?s "TODO")]]"#;

let resp = client.query(query.into(), vec![]).await?;
for row in &resp.result {
    let uid = row[0].as_str().unwrap_or("");
    let text = row[1].as_str().unwrap_or("");
    println!("{}: {}", uid, text);
}
```

### `write`

Execute a write operation (create, update, delete, or move a block).

```rust
pub async fn write(&self, action: WriteAction) -> Result<()>
```

**Parameters:**

* `action` — a `WriteAction` variant describing the mutation

**Example:**

```rust
use roam_sdk::types::*;

client.write(WriteAction::UpdateBlock {
    block: BlockUpdate {
        uid: "abc123".into(),
        string: "Updated content".into(),
    },
}).await?;
```

See [Types](/sdk/types.md) for all `WriteAction` variants.

### `write_batch`

Execute multiple write operations in sequence. Each action is sent as an individual API request, stopping on the first error.

```rust
pub async fn write_batch(&self, actions: Vec<WriteAction>) -> Result<()>
```

**Parameters:**

* `actions` — a list of `WriteAction` variants to execute atomically

**Example:**

```rust
use roam_sdk::types::*;

client.write_batch(vec![
    WriteAction::CreatePage {
        page: PageCreate {
            title: "New Page".into(),
            uid: None,
        },
    },
    WriteAction::CreateBlock {
        location: BlockLocation {
            parent_uid: "page-uid".into(),
            order: OrderValue::Position("last".into()),
        },
        block: NewBlock {
            string: "First block".into(),
            uid: None,
            open: None,
        },
    },
]).await?;
```

Sends each action as a separate API request in order. Stops and returns the error if any request fails.

## Authentication

The client sends the API token as a Bearer token in the `X-Authorization` header on every request. All communication goes over HTTPS via rustls (no OpenSSL needed).

## Base URL

Requests go to `https://api.roamresearch.com/api/graph/{graph_name}/`:

| Endpoint | Method    |
| -------- | --------- |
| `/pull`  | `pull()`  |
| `/q`     | `query()` |
| `/write` | `write()` |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://roam-tui.avelino.run/sdk/client.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
