# RPC protocol

The RPC protocol allows for easy bidirectional communication with SurrealDB.

The RPC protocol allows for network protocol agnostic communication with SurrealDB. It is used internally by our client SDKs, and supports both HTTP and WebSocket based communication. Combined with the power of our [CBOR protocol](./cbor) specification, the RPC protocol provides a fully type-safe and efficient way to interact with SurrealDB over the network.

## Session variables

SurrealDB's session variables provide a robust mechanism for managing session-specific data. Think of them as temporary storage tied directly to a user's active connection, ideal for tasks like maintaining application state, storing user preferences, or holding temporary data relevant only to the current session.

A key characteristic of session variables is their scope: they are strictly confined to the individual connection. This isolation ensures that one user's session data remains private and does not interfere with others, allowing for personalized experiences within a multi-user environment.
You can interact with session variables in the following ways:

1.  **Explicit Session-Wide Management:**
    *   Use the [`let`](#let-) method to define a new variable or update an existing one within the current session. This variable will persist for the duration of the connection.
    *   Use the [`unset`](#unset-) method to remove a previously defined variable from the session.
    *   The [`reset`](#reset) method, in addition to its other functions, clears *all* currently defined session variables, restoring the session's variable state.

2.  **Implicit Request-Scoped Management:**
    *   Methods [`query`](#query), [`select`](#select), [`insert`](#insert), [`create`](#create), [`upsert`](#upsert), [`update`](#update), [`relate`](#relate), and [`delete`](#delete), accept an optional `vars` parameter. This parameter is an object containing key-value pairs, where each key represents the variable name (without the leading `$`) and the value is the data to be assigned.
    *   Variables passed via this parameter are defined *only* for the execution context of that specific method call. They temporarily override any session-wide variable with the same name for that request but do not permanently alter the session state. These variables are automatically discarded once the method execution completes.

To utilize a session variable within a query or method, prefix its name with a dollar sign (`$`), for example, `$user_id`.
## Supported methods

You can use the RPC protocol to perform the following actions:

<table>
    <thead>
        <tr>
            <th scope="col">Function</th>
            <th scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td scope="row" data-label="Function"><a href="#authenticate"><code>authenticate [ token ]</code></a></td>
            <td scope="row" data-label="Description">Authenticate a user against SurrealDB with a token</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#create"><code>create [ thing, data ]</code></a></td>
            <td scope="row" data-label="Description">Create a record with a random or specified ID</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#delete"><code>delete [ thing ]</code></a></td>
            <td scope="row" data-label="Description">Delete either all records in a table or a single record</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#info"><code>info</code></a></td>
            <td scope="row" data-label="Description">Returns the record of an authenticated record user</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#insert"><code>insert [ thing, data ]</code></a></td>
            <td scope="row" data-label="Description">Insert one or multiple records in a table</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#insert_relation"><code>insert_relation [ table, data ]</code></a></td>
            <td scope="row" data-label="Description">Insert a new relation record into a specified table or infer the table from the data</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#invalidate"><code>invalidate</code></a></td>
            <td scope="row" data-label="Description">Invalidate a user's session for the current connection</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#kill-"><code>kill [ queryUuid ]</code></a></td>
            <td scope="row" data-label="Description">Kill an active live query</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#let-"><code>let [ name, value ]</code></a></td>
            <td scope="row" data-label="Description">Define a variable on the current connection</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#live-"><code>live [ table, diff ]</code></a></td>
            <td scope="row" data-label="Description">Initiate a live query</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#merge"><code>merge [ thing, data ]</code></a></td>
            <td scope="row" data-label="Description">Merge specified data into either all records in a table or a single record</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#patch"><code>patch [ thing, patches, diff ]</code></a></td>
            <td scope="row" data-label="Description">Patch either all records in a table or a single record with specified patches</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#ping"><code>ping</code></a></td>
            <td scope="row" data-label="Description">Sends a ping to the database</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#query"><code>query [ sql, vars ]</code></a></td>
            <td scope="row" data-label="Description">Execute a custom query with optional variables</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#relate"><code>relate [ in, relation, out, data? ]</code></a></td>
            <td scope="row" data-label="Description"> Create graph relationships between created records </td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#reset"><code>reset</code></a></td>
            <td scope="row" data-label="Description">Resets all attributes for the current connection</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#run"><code>run [ func_name, version, args ]</code></a></td>
            <td scope="row" data-label="Description">Execute built-in functions, custom functions, or machine learning models with optional arguments.</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#select"><code>select [ thing ]</code></a></td>
            <td scope="row" data-label="Description">Select either all records in a table or a single record</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#signin"><code>signin [NS, DB, AC, ... ]</code></a></td>
            <td scope="row" data-label="Description">Signin a root, NS, DB or record user against SurrealDB</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#signup"><code>signup [ NS, DB, AC, ... ]</code></a></td>
            <td scope="row" data-label="Description">Signup a user using the SIGNUP query defined in a record access method</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#unset-"><code>unset [ name ]</code></a></td>
            <td scope="row" data-label="Description">Remove a variable from the current connection</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#update"><code>update [ thing, data ]</code></a></td>
            <td scope="row" data-label="Description">Modify either all records in a table or a single record with specified data if the record already exists</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#upsert"><code>upsert [ thing, data ]</code></a></td>
            <td scope="row" data-label="Description">Replace either all records in a table or a single record with specified data</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#use"><code>use [ ns, db ]</code></a></td>
            <td scope="row" data-label="Description">Specifies or unsets the namespace and/or database for the current connection</td>
        </tr>
        <tr>
            <td scope="row" data-label="Function"><a href="#version"><code>version</code></a></td>
            <td scope="row" data-label="Description">Returns version information about the database/server</td>
        </tr>
    </tbody>
</table>

<br />

## Message size limits

How much a single RPC call may carry depends on the transport it arrives over.

| Transport | Default limit | Environment variable |
| --------- | ------------- | -------------------- |
| WebSocket `/rpc` | 128 MiB per message | `SURREAL_WEBSOCKET_MAX_MESSAGE_SIZE` |
| HTTP `POST /rpc` | 4 MiB per request body | `SURREAL_HTTP_MAX_RPC_BODY_SIZE` |

The WebSocket ceiling is roughly thirty times the HTTP one, which is why the SDKs default to the WebSocket engine. If a payload is rejected over HTTP, moving the same call onto a WebSocket connection is usually enough.

### Server and client limits

The values above are what the **server** accepts. An SDK may also enforce its own limit on **outgoing** messages, and that limit is applied before anything reaches the network. The effective ceiling is therefore whichever of the two is lower.

The Rust SDK defaults to 64 MiB per message. Sending more than that produces a `Message too long` error from the client itself, not a rejection from the server. The limit is configurable:

```rust
use surrealdb::engine::remote::ws::Ws;
use surrealdb::opt::{Config, WebsocketConfig};
use surrealdb::Surreal;

let websocket = WebsocketConfig::new().max_message_size(128 << 20); // 128 MiB
let config = Config::new().websocket(websocket)?;
let db = Surreal::new::<Ws>(("127.0.0.1:8000", config)).await?;
```

Raising a client limit above the server's ceiling does not help, as the server still rejects the message. Raise both in this case, or keep messages under the lower of the two.

For request bodies on the other HTTP endpoints, see [Request size limits](/docs/reference/rest-api/http-protocol.md#request-size-limits).

<br />

## `authenticate`
This method allows you to authenticate a user against SurrealDB with a token.

```json title="Method Syntax"
authenticate [ token ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>token</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The token that authenticates the user
            </td>
        </tr>
    </tbody>
</table>

### Example usage

```json title="Request"
{
    "id": 1,
    "method": "authenticate",
    "params": [ "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJTdXJyZWFsREIiLCJpYXQiOjE1MTYyMzkwMjIsIm5iZiI6MTUxNjIzOTAyMiwiZXhwIjoxODM2NDM5MDIyLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJTQyI6InVzZXIiLCJJRCI6InVzZXI6dG9iaWUifQ.N22Gp9ze0rdR06McGj1G-h2vu6a6n9IVqUbMFJlOxxA" ]
}
```

```json title="Response"
{
    "id": 1,
    "result": null
}
```

<br />

## `create`

This method creates a record either with a random or specified ID.

```json title="Method Syntax"
create [ thing, data ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>thing</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The thing (Table or Record ID) to create. Passing just a table will result in a randomly generated ID
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>data</code>
               <label label="optional" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The content of the record
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "create",
    "params": [
        "person",
        {
            "name": "Mary Doe"
        }
    ]
}
```

```json title="Response"
{
    "id": 1,
    "result": [
        {
            "id": "person:s5fa6qp4p8ey9k5j0m9z",
            "name": "Mary Doe"
        }
    ]
}
```

<br />

## `delete`

This method deletes either all records in a table or a single record.

```json title="Method Syntax"
delete [ thing ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>record_id</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The record_id (Table or Record ID) to delete
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "delete",
    "params": [ "person:8s0j0bbm3ngrd5c9bx53" ]
}
```

Notice how the deleted record is returned. This differs from a [`DELETE`](/docs/reference/query-language/statements/delete.md) statement via the CLI or SurrealDB Studio which returns nothing unless the `RETURN BEFORE` clause is used.

```json title="Response"
{
    "id": 1,
    "result": {
        "active": true,
        "id": "person:8s0j0bbm3ngrd5c9bx53",
        "last_updated": "2023-06-16T08:34:25Z",
        "name": "John Doe"
    }
}
```

<br />

## `info`

This method returns the record of an authenticated record user.

```json title="Method Syntax"
info
```

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "info"
}
```

The result property of the response is likely different depending on your schema and the authenticated user. However, it does represent the overall structure of the responding message.

```json title="Response"
{
    "id": 1,
    "result": {
        "id": "user:john",
        "name": "John Doe"
    }
}
```

<br />

## `insert`

This method creates a record either with a random or specified ID.

```json title="Method Syntax"
insert [ thing, data ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>thing</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The table to insert in to
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>data</code>
               <label label="optional" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            One or multiple record(s)
            </td>
        </tr>
    </tbody>
</table>

### Example usage

```json title="Request"
{
    "id": 1,
    "method": "insert",
    "params": [
        "person",
        {
            "name": "Mary Doe"
        }
    ]
}
```

```json title="Response"
{
    "id": 1,
    "result": [
        {
            "id": "person:s5fa6qp4p8ey9k5j0m9z",
            "name": "Mary Doe"
        }
    ]
}
```

### Bulk insert

```json title="Request"
{
    "id": 1,
    "method": "insert",
    "params": [
        "person",
        [
            {
                "name": "Mary Doe"
            },
            {
                "name": "John Doe"
            }
        ]
    ]
}
```

```json title="Response"
{
    "id": 1,
    "result": [
        {
            "id": "person:s5fa6qp4p8ey9k5j0m9z",
            "name": "Mary Doe"
        },
        {
            "id": "person:xtbbojcm82a97vus9x0j",
            "name": "John Doe"
        }
    ]
}
```

<br />

## `insert_relation`

This method inserts a new relation record into the database. You can specify the relation table to insert into and provide the data for the new relation.

```json title="Method Syntax"
insert_relation [ table, data ]
```

### Parameters

<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>table</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The name of the relation table to insert into. If `null` or `none`, the table is determined from the `id` field in the `data`.
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>data</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            An object containing the data for the new relation record, including `in`, `out`, and any additional fields.
            </td>
        </tr>
    </tbody>
</table>

### Example usage

**Inserting a Relation into a Specified Table**

```json title="Request"
{
    "id": 1,
    "method": "insert_relation",
    "params": [
        "likes",                   // (relation table)
        {                          // data
            "in": "user:alice",
            "out": "post:123",
            "since": "2024-09-15T12:34:56Z"
        }
    ]
}
```

```json title="Response"
{
    "id": 1,
    "result": {
        "id": "likes:user:alice:post:123",
        "in": "user:alice",
        "out": "post:123",
        "since": "2024-09-15T12:34:56Z"
    }
}
```

**Inserting a Relation Without Specifying the Table**

If you do not specify the `table` parameter (i.e., set it to `null` or `none`), the relation table is inferred from the `id` field within the `data`.

```json title="Request"
{
    "id": 2,
    "method": "insert_relation",
    "params": [
        null,                      // relation table is null
        {                          // data
            "id": "follows:user:alice:user:bob",
            "in": "user:alice",
            "out": "user:bob",
            "since": "2024-09-15T12:34:56Z"
        }
    ]
}
```

```json title="Response"
{
    "id": 2,
    "result": {
        "id": "follows:user:alice:user:bob",
        "in": "user:alice",
        "out": "user:bob",
        "since": "2024-09-15T12:34:56Z"
    }
}
```

### Notes

- **`table` parameter:**
  - Specifies the relation table into which the new relation record will be inserted.
  - If `table` is `null` or `none`, the method expects the `data` to contain an `id` from which it can infer the relation table.

- **`data` parameter:**
  - Must include at least the `in` and `out` fields, representing the starting and ending points of the relation.
  - Can include additional fields to store more information within the relation.

- **Relation IDs:**
  - If an `id` is provided in the `data`, it will be used as the identifier for the new relation record.
  - If no `id` is provided, the system may generate one based on the `table`, `in`, and `out` fields.

- **Single vs. multiple inserts:**
  - The method primarily handles single relation inserts.
  - The `one` variable in the code determines if the `table` parameter refers to a single item.

### Error handling

- **Invalid parameters:**
  - If you provide fewer than two parameters or incorrect parameter types, you will receive an `InvalidParams` error.
  - The method expects exactly two parameters: `table` and `data`.

**Example of invalid parameters:**

```json title="Request with missing parameters"
{
    "id": 3,
    "method": "insert_relation",
    "params": [
        "likes"  // Missing the data parameter
    ]
}
```

```json title="Response"
{
    "id": 3,
    "error": {
        "code": -32602,
        "message": "Invalid parameters"
    }
}
```

### Best practices

- **Include `in` and `out` Fields:**
  - Always provide the `in` and `out` fields in your `data` to define the relation endpoints.

- **Specifying the Relation Table:**
  - If possible, specify the `table` parameter to clearly indicate the relation table.
  - If not specified, ensure that the `id` in `data` correctly reflects the desired relation table.

- **Providing an `id` in `data`:**
  - If you want to control the `id` of the relation, include it in the `data`.
  - This is especially important when `table` is `null` or `none`.

### Additional examples

**Inserting a Relation with Auto-Generated ID**

```json title="Request"
{
    "id": 4,
    "method": "insert_relation",
    "params": [
        "friendship",              // table (relation table)
        {                          // data
            "in": "user:alice",
            "out": "user:bob",
            "since": "2024-09-15"
        }
    ]
}
```

```json title="Response"
{
    "id": 4,
    "result": {
        "id": "friendship:user:alice:user:bob",
        "in": "user:alice",
        "out": "user:bob",
        "since": "2024-09-15"
    }
}
```

**Notes:**

- The `id` is generated based on the `table`, `in`, and `out` fields.
- The relation is inserted into the `friendship` table.

The `insert_relation` method is a powerful way to insert new relation records into your database, allowing you to specify the relation table and include detailed data for each relation. By understanding the parameters and how the method operates, you can effectively manage relationships between records in your database.

> [!NOTE]
> This method is particularly useful in databases that support graph-like relations, enabling complex data modelling and querying capabilities.

<br />

## `invalidate`

This method will invalidate the user's session for the current connection.

```json title="Method Syntax"
invalidate
```

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "invalidate"
}
```

```json title="Response"
{
    "id": 1,
    "result": null
}
```

<br />

## `let` <label label="websocket only" />

This method stores a variable on the current connection.

```json title="Method Syntax"
let [ name, value ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>name</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The name for the variable without a prefixed $ character
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>value</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The value for the variable
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "let",
    "params": [ "website", "https://surrealdb.com/" ]
}
```

```json title="Response"
{
    "id": 1,
    "result": null
}
```

<br />

## `live` <label label="websocket only" />

This methods initiates a live query for a specified table name.

```json title="Method Syntax"
live[ table ]
```

> [!IMPORTANT]
> For more advanced live queries where filters are needed, use the Query method to initiate a custom live query.

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>table</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The table to initiate a live query for
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>diff</code>
               <label label="optional" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                If set to true, live notifications will contain an array of [JSON Patches](https://jsonpatch.com) instead of the entire record
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "live",
    "params": [ "person" ]
}
```

```json title="Response"
{
    "id": 1,
    "result": "0189d6e3-8eac-703a-9a48-d9faa78b44b9"
}
```

### Live notification
For every creation, update or deletion on the specified table, a live notification will be sent. Live notifications do not have an ID attached, but rather include the Live Query's UUID in the result object.

```json
{
    "result": {
        "action": "CREATE",
        "id": "0189d6e3-8eac-703a-9a48-d9faa78b44b9",
        "result": {
            "id": "person:8s0j0bbm3ngrd5c9bx53",
            "name": "John"
        }
    }
}
```

<br />

## `merge`

This method merges specified data into either all records in a table or a single record.

```json title="Method Syntax"
merge [ thing, data ]
```

> [!NOTE]
> This function merges the current document / record data with the specified data. If no merge data is passed it will simply trigger an update.

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>thing</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The thing (Table or Record ID) to merge into
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>data</code>
               <label label="optional" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The content of the record
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "merge",
    "params": [
        "person",
        {
            "active": true
        }
    ]
}
```

```json title="Response"
{
  "id": 1,
  "result": [
      {
          "active": true,
          "id": "person:8s0j0bbm3ngrd5c9bx53",
          "name": "John Doe"
      },
      {
          "active": true,
          "id": "person:s5fa6qp4p8ey9k5j0m9z",
          "name": "Mary Doe"
      }
  ]
}
```

<br />

## `patch`

This method patches either all records in a table or a single record with specified patches.

```json title="Method Syntax"
patch [ thing, patches, diff ]
```

> [!NOTE]
> This function patches the current document / record data with the specified [JSON Patch](https://jsonpatch.com) data.

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>thing</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The thing (Table or Record ID) to patch
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>patches</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            An array of patches following the [JSON Patch specification](https://jsonpatch.com)
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>diff</code>
               <label label="optional" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            A boolean representing if just a diff should be returned.
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "patch",
    "params": [
        "person",
        [
            { "op": "replace", "path": "/last_updated", "value": "2023-06-16T08:34:25Z" }
        ]
    ]
}
```

```json title="Response"
{
    "id": 1,
    "result": [
        [
            {
                "op": "add",
                "path": "/last_updated",
                "value": "2023-06-16T08:34:25Z"
            }
        ],
        [
            {
                "op": "add",
                "path": "/last_updated",
                "value": "2023-06-16T08:34:25Z"
            }
        ]
    ]
}
```

<br />

## `ping`

```json title="Method Syntax"
ping
```

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "ping",
}
```

```json title="Response"
{
  "id": 1,
  "result": null
}
```

<br />

## `query`

This methods sends a custom SurrealQL query.

```json title="Method Syntax"
query [ sql, vars ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2">Parameter</th>
            <th colspan="2">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>sql</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The query to execute against SurrealDB
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>vars</code>
               <label label="optional" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                A set of variables used by the query
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "query",
    "params": [
        "CREATE person SET name = 'John'; SELECT * FROM type::table($tb);",
        {
            "tb": "person"
        }
    ]
}
```

```json title="Response"
{
  "id": 1,
  "result": [
      {
          "status": "OK",
          "time": "152.5µs",
          "result": [
              {
                  "id": "person:8s0j0bbm3ngrd5c9bx53",
                  "name": "John"
              }
          ]
      },
      {
          "status": "OK",
          "time": "32.375µs",
          "result": [
              {
                  "id": "person:8s0j0bbm3ngrd5c9bx53",
                  "name": "John"
              }
          ]
      }
  ]
}
```

## `relate`

This method relates two records with a specified relation.

```json title="Method Syntax"
relate [ in, relation, out, data? ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>in</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The record to relate to
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>relation</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The relation table
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>out</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The record to relate from
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>data</code>
               <label label="optional" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The content of the record
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "relate",
    "params": [
        "person:12s0j0bbm3ngrd5c9bx53",
        "knows",
        "person:8s0j0bbm3ngrd5c9bx53"
    ]
}
```

```json title="Response"
{
    "id": 1,
    "result": {
        "id": "knows:12s0j0bbm3ngrd5c9bx53:8s0j0bbm3ngrd5c9bx53",
        "in": "person:12s0j0bbm3ngrd5c9bx53",
        "out": "person:8s0j0bbm3ngrd5c9bx53"
    }
}
```

### Creating a relation with additional data

```json title="Request"
{
    "id": 2,
    "method": "relate",
    "params": [
        "person:john_doe",          // in
        "knows",                    // relation
        "person:jane_smith",        // out
        { "since": "2020-01-01" }   // data
    ]
}
```

```json title="Response"
{
    "id": 2,
    "result": {
        "id": "knows:person:john_doe:person:jane_smith",
        "in": "person:jane_smith",
        "out": "person:john_doe",
        "since": "2020-01-01"
    }
}
```

<br />

## `reset`

This method will reset all attributes for the current connection. It clears authentication (much like invalidate), unsets the selected NS/DB, unsets any defined connection params, and aborts any active live queries. On WebSocket connections it also cancels open client-managed transactions for the session being reset and frees their slots under [`SURREAL_MAX_TRANSACTIONS_PER_CONNECTION`](/docs/reference/cli/surrealdb-cli/environment-variables.md#websocket-config) / [`SURREAL_MAX_TRANSACTIONS_PER_SESSION`](/docs/reference/cli/surrealdb-cli/environment-variables.md#websocket-config).

```json title="Method Syntax"
reset
```

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "reset"
}
```

```json title="Response"
{
    "id": 1,
    "result": null
}
```

<br />

## `run`

This method allows you to execute built-in functions, custom functions, or machine learning models with optional arguments.

```json title="Method Syntax"
run [ func_name, version?, args? ]
```

### Parameters

<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>func_name</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The name of the function or model to execute. Prefix with `fn::` for custom functions or `ml::` for machine learning models.
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>version</code>
               <label label="optional" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The version of the function or model to execute.
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>args</code>
               <label label="optional" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The arguments to pass to the function or model.
            </td>
        </tr>
    </tbody>
</table>

### Executing a built-in function

```json title="Request"
{
    "id": 1,
    "method": "run",
    "params": [ "time::now" ]
}
```

```json title="Response"
{
    "id": 1,
    "result": "2024-09-15T12:34:56Z"
}
```

### Executing a custom function

```json title="Request"
{
    "id": 1,
    "method": "run",
    "params": [ "fn::calculate_discount", null, [ 100, 15 ] ]
}
```

```json title="Response"
{
    "id": 1,
    "result": 85
}
```

### Executing a machine learning model

```json title="Request"
{
    "id": 1,
    "method": "run",
    "params": [ "ml::image_classifier", "v2.1", [ "image_data_base64" ] ]
}
```

```json title="Response"
{
    "id": 1,
    "result": "cat"
}
```

> [!IMPORTANT]
> When using a machine learning model (prefixed with `ml::`), the `version` parameter is **required**.

<br />

## `select`

This method selects either all records in a table or a single record.

```json title="Method Syntax"
select [ thing ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>thing</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The thing (Table or Record ID) to select
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "select",
    "params": [ "person" ]
}
```

```json title="Response"
{
    "id": 1,
    "result": [
        {
            "id": "person:8s0j0bbm3ngrd5c9bx53",
            "name": "John"
        }
    ]
}
```

<br />

## `signin`

This method allows you to sign in as a root, namespace, or database user, or with a record access method.

The object returned will contain a `token` property and an optional `refresh` property.

```json title="Method Syntax"
signin [ NS, DB, AC, ... ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>NS</code>
                <label label="required"></label>
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The namespace to sign in to. Only required for `DB & RECORD` authentication
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>DB</code>
                <label label="required"></label>
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The database to sign in to. Only required for `RECORD` authentication
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>AC</code>
                <label label="required"></label>
            </td>
            <td colspan="2" scope="row" data-label="Description">
                Specifies the access method. Only required for `RECORD` authentication
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>user</code>
                <label label="required">REQUIRED FOR ROOT, NS & DB</label>
            </td>
            <td colspan="2" scope="row" data-label="Description">
            	The username of the database user. Only required for `ROOT, NS & DB` authentication
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>pass</code>
                <label label="required">REQUIRED FOR ROOT, NS & DB</label>
            </td>
            <td colspan="2" scope="row" data-label="Description">
            	The password of the database user. Only required for `ROOT, NS & DB` authentication
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>...</code>
            </td>
            <td colspan="2" scope="row" data-label="Description">
                Specifies any variables to pass to the `SIGNIN` query. Only relevant for `RECORD` authentication
            </td>
        </tr>
    </tbody>
</table>

### Example with root user

```json title="Request"
{
    "id": 1,
    "method": "signin",
    "params": [
        {
            "user": "tobie",
            "pass": "3xtr3m3ly-s3cur3-p@ssw0rd"
        }
    ]
}
```

```json title="Response"
{
    "id": 1,
    "result": null
}
```

### Example with record user

```json title="Request"
{
    "id": 1,
    "method": "signin",
    "params": [
        {
            "NS": "surrealdb",
            "DB": "docs",
            "AC": "commenter",

            "username": "johndoe",
            "password": "SuperStrongPassword!"
        }
    ]
}
```

```json title="Response"
{
    "id": 1,
    "result": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJTdXJyZWFsREIiLCJpYXQiOjE1MTYyMzkwMjIsIm5iZiI6MTUxNjIzOTAyMiwiZXhwIjoxODM2NDM5MDIyLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJTQyI6InVzZXIiLCJJRCI6InVzZXI6dG9iaWUifQ.N22Gp9ze0rdR06McGj1G-h2vu6a6n9IVqUbMFJlOxxA"
}
```

<br />

## `signup`

This method allows you to sign a user up using the `SIGNUP` query defined in a record access method.

The object returned will contain an optional `token` property and an optional `refresh` property.

```json title="Method Syntax"
signup [ NS, DB, AC, ... ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>NS</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                Specifies the namespace of the record access method
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>DB</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                Specifies the database of the record access method
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>AC</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                Specifies the access method
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>...</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                Specifies any variables used by the SIGNUP query of the record access method
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "signup",
    "params": [
        {
            "NS": "surrealdb",
            "DB": "docs",
            "AC": "commenter",

            "username": "johndoe",
            "password": "SuperStrongPassword!"
        }
    ]
}
```

```json title="Response"
{
  "id": 1,
  "result": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJTdXJyZWFsREIiLCJpYXQiOjE1MTYyMzkwMjIsIm5iZiI6MTUxNjIzOTAyMiwiZXhwIjoxODM2NDM5MDIyLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJTQyI6InVzZXIiLCJJRCI6InVzZXI6dG9iaWUifQ.N22Gp9ze0rdR06McGj1G-h2vu6a6n9IVqUbMFJlOxxA"
}
```

<br />

## `unset` <label label="websocket only" />

This method removes a variable from the current connection.

```json title="Method Syntax"
unset [ name ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>name</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The name of the variable without a prefixed $ character
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "unset",
    "params": [ "website" ]
}
```

```json title="Response"
{
    "id": 1,
    "result": null
}
```

<br />

## `update`

This method replaces either all records in a table or a single record with specified data.

```json title="Method Syntax"
update [ thing, data ]
```

> [!NOTE]
> This function replaces the current document / record data with the specified data if that document / record has already been created. If no document has been created this will return an empty array. Also, if no replacement data is passed it will simply trigger an update.

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>thing</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The thing (Table or Record ID) to update
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>data</code>
               <label label="optional" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The content of the record
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "update",
    "params": [
        "person:8s0j0bbm3ngrd5c9bx53",
        {
            "name": "John Doe"
        }
    ]
}
```

```json title="Response"
{
    "id": 1,
    "result": {
        "id": "person:8s0j0bbm3ngrd5c9bx53",
        "name": "John Doe"
    }
}
```

<br />

## `upsert`

```json title="Method Syntax"
upsert [ thing, data ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>thing</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The thing (Table or Record ID) to upsert
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Parameter">
                <code>data</code>
               <label label="optional" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
            The content of the record
            </td>
        </tr>
    </tbody>
</table>

### Example usage
```json title="Request"
{
    "id": 1,
    "method": "upsert",
    "params": [
        "person:12s0j0bbm3ngrd5c9bx53",
        {
            "name": "John Doe",
            "job": "Software developer",
        }
    ]
}
```

```json title="Response"
{
    "id": 1,
    "result": {
        "id": "person:12s0j0bbm3ngrd5c9bx53",
        "name": "John Doe",
        "job": "Software developer"
    }
}
```

<br />

## `use`

This method specifies or unsets the namespace and/or database for the current connection.

```json title="Method Syntax"
use [ ns, db ]
```

### Parameters
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Parameter</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Function">
                <code>NS</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                Sets the selected Namespace for queries
            </td>
        </tr>
        <tr>
            <td colspan="2" scope="row" data-label="Function">
                <code>DB</code>
                <label label="required" />
            </td>
            <td colspan="2" scope="row" data-label="Description">
                Sets the selected Database for queries
            </td>
        </tr>
    </tbody>
</table>

### Accepted values

For either the namespace or database, a string will change the value, `null` will unset the value, and `none` will cause the value to not be affected.

### Example usage

```json title="Request"
{
    "id": 1,
    "method": "use",
    "params": [ "surrealdb", "docs" ]
}
```

```json title="Response"
{
    "id": 1,
    "result": null
}
```

```surql title="Example Combinations"
[none, none]     -- Won't change ns or db
["test", none]   -- Change ns to test
[none, "test"]   -- Change db to test
["test", "test"] -- Change ns and db to test

[none, null]     -- Will only unset the database
[null, none]     -- Will throw an error, you cannot unset only the database
[null, null]     -- Will unset both ns and db
["test", null]   -- Change ns to test and unset db
```

<br />

## `version`

This method returns version information about the database/server.

```json title="Method Syntax"
version
```

### Parameters

This method does not accept any parameters.

### Example usage

```json title="Request"
{
    "id": 1,
    "method": "version"
}
```

```json title="Response"
{
    "id": 1,
    "result": {
        "version": "3.2.0",
        "build": "abc123",
        "timestamp": "2024-09-15T12:34:56Z"
    }
}
```

### Notes

- **Parameters:** Providing any parameters will result in an `InvalidParams` error.
- **Result Fields:**
  - `version`: The version number of the database/server.
  - `build`: The build identifier.
  - `timestamp`: The timestamp when the version was built or released.

> [!NOTE]
> The actual values in the response will depend on your specific database/server instance.

<br />
