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 specification, the RPC protocol provides a fully type-safe and efficient way to interact with SurrealDB over the network.
NoteVersion 2 of the RPC protocol significantly enhances data manipulation and retrieval methods by adding an optional object parameter. This enables direct control over statement clauses like
WHERE,LIMIT,TIMEOUT, and more, previously only available through direct SQL execution.
ImportantMethod Consolidation: The
merge,patch, andinsert_relationmethods from version 1 have been consolidated into theupdateandinsertmethods respectively.
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:
Explicit Session-Wide Management:
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.unset method to remove a previously defined variable from the session.reset method, in addition to its other functions, clears all currently defined session variables, restoring the session’s variable state.Implicit Request-Scoped Management:
query, select, insert, create, upsert, update, relate, and 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.To utilize a session variable within a query or method, prefix its name with a dollar sign ($), for example, $user_id.
You can use the RPC protocol to perform the following actions:
| Function | Description | 
|---|---|
| authenticate [ token ] | Authenticate a user against SurrealDB with a token | 
| create [ thing, data ] | Create a record with a random or specified ID | 
| delete [ thing ] | Delete either all records in a table or a single record | 
| graphql [ query, options? ] | Execute GraphQL queries against the database | 
| info | Returns the record of an authenticated record user | 
| insert [ thing, data ] | Insert one or multiple records in a table | 
| insert_relation [ table, data ] | Insert a new relation record into a specified table or infer the table from the data | 
| invalidate | Invalidate a user’s session for the current connection | 
| kill [ queryUuid ] | Kill an active live query | 
| let [ name, value ] | Define a variable on the current connection | 
| live [ table, diff ] | Initiate a live query | 
| merge [ thing, data ] | Merge specified data into either all records in a table or a single record | 
| patch [ thing, patches, diff ] | Patch either all records in a table or a single record with specified patches | 
| ping | Sends a ping to the database | 
| query [ sql, vars ] | Execute a custom query with optional variables | 
| relate [ in, relation, out, data? ] | Create graph relationships between created records | 
| reset | Resets all attributes for the current connection | 
| run [ func_name, version, args ] | Execute built-in functions, custom functions, or machine learning models with optional arguments. | 
| select [ thing ] | Select either all records in a table or a single record | 
| signin [NS, DB, AC, … ] | Signin a root, NS, DB or record user against SurrealDB | 
| signup [ NS, DB, AC, … ] | Signup a user using the SIGNUP query defined in a record access method | 
| unset [ name ] | Remove a variable from the current connection | 
| update [ thing, data ] | Modify either all records in a table or a single record with specified data if the record already exists | 
| upsert [ thing, data ] | Replace either all records in a table or a single record with specified data | 
| use [ ns, db ] | Specifies or unsets the namespace and/or database for the current connection | 
| version | Returns version information about the database/server | 
authenticateThis method allows you to authenticate a user against SurrealDB with a token.
| Parameter | Description | ||
|---|---|---|---|
| tokenrequired | The token that authenticates the user | 
createThis method creates a record either with a random or specified ID.
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | The thing (Table or Record ID) to create. Passing just a table will result in a randomly generated ID | ||
| dataoptional | The content of the record | 
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | |||
| dataoptional | |||
| onlyoptional | corresponds to  | ||
| outputoptional | corresponds to  | ||
| timeoutoptional | corresponds to  | ||
| versionoptional | corresponds to  | ||
| varsoptional | 
This section is under development. The functionality is closely aligned with the CREATE statement. For more details, refer to the CREATE statement documentation.
deleteThis method deletes either all records in a table or a single record.
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | The thing (Table or Record ID) to delete | 
Notice how the deleted record is being returned here
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | |||
| onlyoptional | A boolean, stating where we want to select or affect only a single record. | ||
| outputoptional | By default, the delete method returns nothing. To change what is returned, we can use the output option, specifying either “none”, “null”, “diff”, “before”, “after”. | ||
| timeoutoptional | corresponds to  | ||
| varsoptional | 
This section is under development. The functionality is closely aligned with the DELETE statement. For more details, refer to the DELETE statement documentation.
graphqlAvailable since: v2.0.0
This method allows you to execute GraphQL queries against the database.
| Parameter | Description | ||
|---|---|---|---|
| queryrequired | A GraphQL query string or an object containing the query, variables, and operation name. | ||
| optionsoptional | An object specifying options such as the output format and pretty-printing. | 
query ParameterThe query parameter can be either:
{ "query": "{ author { id name } }" }
query (required): The GraphQL query string.variables or vars (optional): An object containing variables for the query.operationName or operation (optional): The name of the operation to execute.Example:
options ParameterThe options parameter is an object that may include:
pretty (optional, default false): A boolean indicating whether the output should be pretty-printed.format (optional, default "json"): The response format. Currently, only "json" is supported.Example:
MethodNotFound error."json" format is supported. Using "cbor" will result in an error.pretty to true formats the JSON response with indentation for readability.ImportantEnsure that your GraphQL queries and variables are correctly formatted to avoid parsing errors.
infoThis method returns the record of an authenticated record user.
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.
insertThis method creates a record either with a random or specified ID.
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | The table to insert in to | ||
| dataoptional | One or multiple record(s) | 
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | |||
| datarequired | |||
| data_exproptional | Specifies how the  
 | ||
| relationoptional | A boolean indicating whether the inserted records are relations. | ||
| outputoptional | corresponds to  | ||
| timeoutoptional | A duration, stating how long the statement is run within the database before timing out. | ||
| versionoptional | If you are using SurrealKV as the storage engine with versioning enabled, when creating a record you can specify a version for each record. | ||
| varsoptional | 
This section is under development. The functionality is closely aligned with the INSERT statement. For more details, refer to the INSERT statement documentation.
insert_relationThis 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.
| Parameter | Description | ||
|---|---|---|---|
| tablerequired | The name of the relation table to insert into. If  | ||
| datarequired | An object containing the data for the new relation record, including  | 
Inserting a Relation into a Specified Table
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.
table Parameter:
table is null or none, the method expects the data to contain an id from which it can infer the relation table.data Parameter:
in and out fields, representing the starting and ending points of the relation.Relation IDs:
id is provided in the data, it will be used as the identifier for the new relation record.id is provided, the system may generate one based on the table, in, and out fields.Single vs. Multiple Inserts:
one variable in the code determines if the table parameter refers to a single item.InvalidParams error.table and data.Example of Invalid Parameters:
Include in and out Fields:
in and out fields in your data to define the relation endpoints.Specifying the Relation Table:
table parameter to clearly indicate the relation table.id in data correctly reflects the desired relation table.Providing an id in data:
id of the relation, include it in the data.table is null or none.Inserting a Relation with Auto-Generated ID
Notes:
id is generated based on the table, in, and out fields.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.
NoteThis method is particularly useful in databases that support graph-like relations, enabling complex data modeling and querying capabilities.
invalidateThis method will invalidate the user’s session for the current connection.
kill websocket only This method kills an active live query
| Parameter | Description | ||
|---|---|---|---|
| queryUuidrequired | The UUID of the live query to kill | 
let websocket only This method stores a variable on the current connection.
| Parameter | Description | ||
|---|---|---|---|
| namerequired | The name for the variable without a prefixed $ character | ||
| valuerequired | The value for the variable | 
live websocket only This methods initiates a live query for a specified table name.
ImportantFor more advanced live queries where filters are needed, use the Query method to initiate a custom live query.
| Parameter | Description | ||
|---|---|---|---|
| tablerequired | The table to initiate a live query for | ||
| diffoptional | If set to true, live notifications will contain an array of JSON Patches instead of the entire record | 
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.
mergeThis method merges specified data into either all records in a table or a single record.
NoteThis function merges the current document / record data with the specified data. If no merge data is passed it will simply trigger an update.
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | The thing (Table or Record ID) to merge into | ||
| dataoptional | The content of the record | 
patchThis method patches either all records in a table or a single record with specified patches.
NoteThis function patches the current document / record data with the specified JSON Patch data.
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | The thing (Table or Record ID) to patch | ||
| patchesrequired | An array of patches following the JSON Patch specification | ||
| diffoptional | A boolean representing if just a diff should be returned. | 
ping
queryThis methods sends a custom SurrealQL query.
| Parameter | Description | ||
|---|---|---|---|
| sqlrequired | The query to execute against SurrealDB | ||
| varsoptional | A set of variables used by the query | 
relateAvailable since: v1.5.0
This method relates two records with a specified relation.
| Parameter | Description | ||
|---|---|---|---|
| inrequired | The record to relate to | ||
| relationrequired | The relation table | ||
| outrequired | The record to relate from | ||
| dataoptional | The content of the record | 
| Parameter | Description | ||
|---|---|---|---|
| inrequired | The record to relate from | ||
| relationrequired | The relation table | ||
| outrequired | The record to relate to | ||
| dataoptional | |||
| uniqueoptional | A boolean, stating wether the relation we are inserting needs to be unique. | ||
| onlyoptional | corresponds to  | ||
| outputoptional | corresponds to  | ||
| timeoutoptional | corresponds to  | ||
| varsoptional | 
This section is under development. The functionality is closely aligned with the RELATE statement. For more details, refer to the RELATE statement documentation.
resetThis method will reset all attributes for the current connection. reset your authentication (much like invalidate) unsets the selected NS/DB, unsets any defined connection params, and aborts any active live queries.
runThis method allows you to execute built-in functions, custom functions, or machine learning models with optional arguments.
| Parameter | Description | ||
|---|---|---|---|
| func_namerequired | The name of the function or model to execute. Prefix with  | ||
| versionoptional | The version of the function or model to execute. | ||
| argsoptional | The arguments to pass to the function or model. | 
ImportantWhen using a machine learning model (prefixed with
ml::), theversionparameter is required.
selectThis method selects either all records in a table or a single record.
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | The thing (Table or Record ID) to select | 
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | |||
| fieldsoptional | |||
| onlyoptional | corresponds to  | ||
| startoptional | A number, stating how many records to skip in a selection. | ||
| limitoptional | corresponds to  | ||
| condoptional | corresponds to  | ||
| timeoutoptional | corresponds to  | ||
| versionoptional | corresponds to  | ||
| varsoptional | 
This section is under development. The functionality is closely aligned with the SELECT statement. For more details, refer to the SELECT statement documentation.
signinThis method allows you to sign in as a root, namespace, or database user, or with a record access method.
ImportantUnlike v1, v2 returns an object containing a
tokenproperty and an optionalrefreshproperty.
| Parameter | Description | ||
|---|---|---|---|
| NSrequired | The namespace to sign in to. Only required for  | ||
| DBrequired | The database to sign in to. Only required for  | ||
| ACrequired | Specifies the access method. Only required for  | ||
| userrequired | The username of the database user. Only required for  | ||
| passrequired | The password of the database user. Only required for  | ||
| … | Specifies any variables to pass to the  | 
signupThis method allows you to sign a user up using the SIGNUP query defined in a record access method.
ImportantUnlike v1, v2 returns an object containing an optional
tokenproperty and an optionalrefreshproperty.
| Parameter | Description | ||
|---|---|---|---|
| NSrequired | Specifies the namespace of the record access method | ||
| DBrequired | Specifies the database of the record access method | ||
| ACrequired | Specifies the access method | ||
| …required | Specifies any variables used by the SIGNUP query of the record access method | 
unset websocket only This method removes a variable from the current connection.
| Parameter | Description | ||
|---|---|---|---|
| namerequired | The name of the variable without a prefixed $ character | 
updateThis method replaces either all records in a table or a single record with specified data.
NoteThis 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.
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | The thing (Table or Record ID) to update | ||
| dataoptional | The content of the record | 
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | |||
| dataoptional | |||
| data_exproptional | |||
| onlyoptional | A boolean, stating where we want to select or affect only a single record. | ||
| condoptional | corresponds to  | ||
| outputoptional | corresponds to  | ||
| timeoutoptional | corresponds to  | ||
| varsoptional | |||
This section is under development. The functionality is closely aligned with the UPDATE statement. For more details, refer to the UPDATE statement documentation.
upsert
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | The thing (Table or Record ID) to upsert | ||
| dataoptional | The content of the record | 
| Parameter | Description | ||
|---|---|---|---|
| thingrequired | |||
| dataoptional | |||
| data_exproptional | |||
| onlyoptional | corresponds to  | ||
| condoptional | corresponds to  | ||
| outputoptional | corresponds to  | ||
| timeoutoptional | A duration, stating how long the statement is run within the database before timing out. | ||
| varsoptional | |||
This section is under development. The functionality is closely aligned with the UPSERT statement. For more details, refer to the UPSERT statement documentation.
useThis method specifies or unsets the namespace and/or database for the current connection.
| Parameter | Description | ||
|---|---|---|---|
| NSrequired | Sets the selected Namespace for queries | ||
| DBrequired | Sets the selected Database for queries | 
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.
versionThis method returns version information about the database/server.
This method does not accept any parameters.
InvalidParams error.version: The version number of the database/server.build: The build identifier.timestamp: The timestamp when the version was built or released.NoteThe actual values in the response will depend on your specific database/server instance.