The RPC protocol allows for easy bi-directional communication with SurrealDB. This allows you to maintain a single connection to run all your queries, but also opens up the possibility of Live Queries!
Function | Description |
---|---|
use [ ns, db ] | Specifies or unsets the namespace and/or database for the current connection |
info | Returns the record of an authenticated record user |
version | Returns version information about the database/server |
signup [ NS, DB, AC, … ] | Signup a user using the SIGNUP query defined in a record access method |
signin [NS, DB, AC, … ] | Signin a root, NS, DB or record user against SurrealDB |
authenticate [ token ] | Authenticate a user against SurrealDB with a token |
invalidate | Invalidate a user’s session for the current connection |
let [ name, value ] | Define a variable on the current connection |
unset [ name ] | Remove a variable from the current connection |
live [ table, diff ] | Initiate a live query |
kill [ queryUuid ] | Kill an active live query |
query [ sql, vars ] | Execute a custom query with optional variables |
graphql [ query, options? ] | Execute GraphQL queries against the database |
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 |
create [ thing, data ] | Create a record with a random or specified ID |
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 |
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 |
relate [ in, relation, out, data? ] | Create graph relationships between created records |
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 |
delete [ thing ] | Delete either all records in a table or a single record |
use
This method specifies or unsets the namespace and/or database for the current connection
Method Syntaxuse [ ns, db ]
Parameter | Description | ||
---|---|---|---|
NS required | Sets the selected Namespace for queries | ||
DB required | 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.
Request{ "id": 1, "method": "use", "params": [ "surrealdb", "docs" ] }
Response{ "id": 1, "result": null }
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
info
This method returns the record of an authenticated record user.
Method Syntaxinfo
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.
Response{ "id": 1, "result": { "id": "user:john", "name": "John Doe" } }
version
This method returns version information about the database/server.
Method Syntaxversion
This method does not accept any parameters.
Request{ "id": 1, "method": "version" }
Response{ "id": 1, "result": { "version": "2.0.0-beta.2", "build": "abc123", "timestamp": "2024-09-15T12:34:56Z" } }
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.
signup
This method allows you to sign a user up using the SIGNUP
query defined in a record access method
Method Syntaxsignup [ NS, DB, AC, ... ]
Parameter | Description | ||
---|---|---|---|
NS required | Specifies the namespace of the record access method | ||
DB required | Specifies the database of the record access method | ||
AC required | Specifies the access method | ||
… required | Specifies any variables used by the SIGNUP query of the record access method |
Request{ "id": 1, "method": "signup", "params": [ { "NS": "surrealdb", "DB": "docs", "AC": "commenter", "username": "johndoe", "password": "SuperStrongPassword!" } ] }
Response{ "id": 1, "result": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJTdXJyZWFsREIiLCJpYXQiOjE1MTYyMzkwMjIsIm5iZiI6MTUxNjIzOTAyMiwiZXhwIjoxODM2NDM5MDIyLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJTQyI6InVzZXIiLCJJRCI6InVzZXI6dG9iaWUifQ.N22Gp9ze0rdR06McGj1G-h2vu6a6n9IVqUbMFJlOxxA" }
signin
This method allows you to sign in as a root, namespace, or database user, or with a record access method
Method Syntaxsignin [ NS, DB, AC, ... ]
Parameter | Description | ||
---|---|---|---|
NS required | The namespace to sign in to. Only required for | ||
DB required | The database to sign in to. Only required for | ||
AC required | Specifies the access method. Only required for | ||
user required | The username of the database user. Only required for | ||
pass required | The password of the database user. Only required for | ||
… | Specifies any variables to pass to the |
Request{ "id": 1, "method": "signin", "params": [ { "user": "tobie", "pass": "3xtr3m3ly-s3cur3-p@ssw0rd" } ] }
Response{ "id": 1, "result": null }
Request{ "id": 1, "method": "signin", "params": [ { "NS": "surrealdb", "DB": "docs", "AC": "commenter", "username": "johndoe", "password": "SuperStrongPassword!" } ] }
Response{ "id": 1, "result": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJTdXJyZWFsREIiLCJpYXQiOjE1MTYyMzkwMjIsIm5iZiI6MTUxNjIzOTAyMiwiZXhwIjoxODM2NDM5MDIyLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJTQyI6InVzZXIiLCJJRCI6InVzZXI6dG9iaWUifQ.N22Gp9ze0rdR06McGj1G-h2vu6a6n9IVqUbMFJlOxxA" }
authenticate
This method allows you to authenticate a user against SurrealDB with a token
Method Syntaxauthenticate [ token ]
Parameter | Description | ||
---|---|---|---|
token required | The token that authenticates the user |
Request{ "id": 1, "method": "authenticate", "params": [ "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJTdXJyZWFsREIiLCJpYXQiOjE1MTYyMzkwMjIsIm5iZiI6MTUxNjIzOTAyMiwiZXhwIjoxODM2NDM5MDIyLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJTQyI6InVzZXIiLCJJRCI6InVzZXI6dG9iaWUifQ.N22Gp9ze0rdR06McGj1G-h2vu6a6n9IVqUbMFJlOxxA" ] }
Response{ "id": 1, "result": null }
invalidate
This method will invalidate the user’s session for the current connection
Method Syntaxinvalidate
Request{ "id": 1, "method": "invalidate" }
Response{ "id": 1, "result": null }
let
websocket only This method stores a variable on the current connection
Method Syntaxlet [ name, value ]
Parameter | Description | ||
---|---|---|---|
name required | The name for the variable without a prefixed $ character | ||
value required | The value for the variable |
Request{ "id": 1, "method": "let", "params": [ "website", "https://surrealdb.com/" ] }
Response{ "id": 1, "result": null }
unset
websocket only This method removes a variable from the current connection
Method Syntaxunset [ name ]
Parameter | Description | ||
---|---|---|---|
name required | The name of the variable without a prefixed $ character |
Request{ "id": 1, "method": "unset", "params": [ "website" ] }
Response{ "id": 1, "result": null }
live
websocket only This methods initiates a live query for a specified table name
Method Syntaxlive[ table ]
ImportantFor more advanced live queries where filters are needed, use the Query method to initiate a custom live query.
Parameter | Description | ||
---|---|---|---|
table required | The table to initiate a live query for | ||
diff optional | If set to true, live notifications will contain an array of JSON Patches instead of the entire record |
Request{ "id": 1, "method": "live", "params": [ "person" ] }
Response{ "id": 1, "result": "0189d6e3-8eac-703a-9a48-d9faa78b44b9" }
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.
{ "result": { "action": "CREATE", "id": "0189d6e3-8eac-703a-9a48-d9faa78b44b9", "result": { "id": "person:8s0j0bbm3ngrd5c9bx53", "name": "John" } } }
kill
websocket only This method kills an active live query
Method Syntaxkill [ queryUuid ]
Parameter | Description | ||
---|---|---|---|
queryUuid required | The UUID of the live query to kill |
Request{ "id": 1, "method": "kill", "params": [ "0189d6e3-8eac-703a-9a48-d9faa78b44b9" ] }
Response{ "id": 1, "result": null }
query
This methods sends a custom SurrealQL query
Method Syntaxquery [ sql, vars ]
Parameter | Description | ||
---|---|---|---|
sql required | The query to execute against SurrealDB | ||
vars optional | A set of variables used by the query |
Request{ "id": 1, "method": "query", "params": [ "CREATE person SET name = 'John'; SELECT * FROM type::table($tb);", { "tb": "person" } ] }
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" } ] } ] }
run
This method allows you to execute built-in functions, custom functions, or machine learning models with optional arguments.
Method Syntaxrun [ func_name, version?, args? ]
Parameter | Description | ||
---|---|---|---|
func_name required | The name of the function or model to execute. Prefix with | ||
version optional | The version of the function or model to execute. | ||
args optional | The arguments to pass to the function or model. |
Request{ "id": 1, "method": "run", "params": [ "time::now" ] }
Response{ "id": 1, "result": "2024-09-15T12:34:56Z" }
Request{ "id": 1, "method": "run", "params": [ "fn::calculate_discount", null, [ 100, 15 ] ] }
Response{ "id": 1, "result": 85 }
Request{ "id": 1, "method": "run", "params": [ "ml::image_classifier", "v2.1", [ "image_data_base64" ] ] }
Response{ "id": 1, "result": "cat" }
ImportantWhen using a machine learning model (prefixed with
ml::
), theversion
parameter is required.
graphql
Available since: v2.0.0
This method allows you to execute GraphQL queries against the database.
Method Syntaxgraphql [ query, options? ]
Parameter | Description | ||
---|---|---|---|
query required | A GraphQL query string or an object containing the query, variables, and operation name. | ||
options optional | 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:
{ "query": "query GetUser($id: ID!) { user(id: $id) { id name } }", "variables": { "id": "user:1" }, "operationName": "GetUser" }
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:
{ "pretty": true, "format": "json" }
Request{ "id": 1, "method": "graphql", "params": [ "{ users { id name } }" ] }
Response{ "id": 1, "result": { "data": { "users": [ { "id": "user:1", "name": "Alice" }, { "id": "user:2", "name": "Bob" } ] } } }
Request{ "id": 1, "method": "graphql", "params": [ { "query": "query GetUser($id: ID!) { user(id: $id) { id name } }", "variables": { "id": "user:1" }, "operationName": "GetUser" } ] }
Response{ "id": 1, "result": { "data": { "user": { "id": "user:1", "name": "Alice" } } } }
Request{ "id": 1, "method": "graphql", "params": [ "{ users { id name } }", { "pretty": true } ] }
Response{ "id": 1, "result": "{\n \"data\": {\n \"users\": [\n { \"id\": \"user:1\", \"name\": \"Alice\" },\n { \"id\": \"user:2\", \"name\": \"Bob\" }\n ]\n }\n}" }
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.
select
This method selects either all records in a table or a single record
Method Syntaxselect [ thing ]
Parameter | Description | ||
---|---|---|---|
thing required | The thing (Table or Record ID) to select |
Request{ "id": 1, "method": "select", "params": [ "person" ] }
Response{ "id": 1, "result": [ { "id": "person:8s0j0bbm3ngrd5c9bx53", "name": "John" } ] }
create
This method creates a record either with a random or specified ID
Method Syntaxcreate [ thing, data ]
Parameter | Description | ||
---|---|---|---|
thing required | The thing (Table or Record ID) to create. Passing just a table will result in a randomly generated ID | ||
data optional | The content of the record |
Request{ "id": 1, "method": "create", "params": [ "person", { "name": "Mary Doe" } ] }
Response{ "id": 1, "result": [ { "id": "person:s5fa6qp4p8ey9k5j0m9z", "name": "Mary Doe" } ] }
insert
This method creates a record either with a random or specified ID
Method Syntaxinsert [ thing, data ]
Parameter | Description | ||
---|---|---|---|
thing required | The table to insert in to | ||
data optional | One or multiple record(s) |
Request{ "id": 1, "method": "insert", "params": [ "person", { "name": "Mary Doe" } ] }
Response{ "id": 1, "result": [ { "id": "person:s5fa6qp4p8ey9k5j0m9z", "name": "Mary Doe" } ] }
Request{ "id": 1, "method": "insert", "params": [ "person", [ { "name": "Mary Doe" }, { "name": "John Doe" } ] ] }
Response{ "id": 1, "result": [ { "id": "person:s5fa6qp4p8ey9k5j0m9z", "name": "Mary Doe" }, { "id": "person:xtbbojcm82a97vus9x0j", "name": "John Doe" } ] }
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.
Method Syntaxinsert_relation [ table, data ]
Parameter | Description | ||
---|---|---|---|
table required | The name of the relation table to insert into. If | ||
data required | An object containing the data for the new relation record, including |
Inserting a Relation into a Specified Table
Request{ "id": 1, "method": "insert_relation", "params": [ "likes", // (relation table) { // data "in": "user:alice", "out": "post:123", "since": "2024-09-15T12:34:56Z" } ] }
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
.
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" } ] }
Response{ "id": 2, "result": { "id": "follows:user:alice:user:bob", "in": "user:alice", "out": "user:bob", "since": "2024-09-15T12:34:56Z" } }
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:
Request with missing parameters{ "id": 3, "method": "insert_relation", "params": [ "likes" // Missing the data parameter ] }
Response{ "id": 3, "error": { "code": -32602, "message": "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
Request{ "id": 4, "method": "insert_relation", "params": [ "friendship", // table (relation table) { // data "in": "user:alice", "out": "user:bob", "since": "2024-09-15" } ] }
Response{ "id": 4, "result": { "id": "friendship:user:alice:user:bob", "in": "user:alice", "out": "user:bob", "since": "2024-09-15" } }
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.
update
This method replaces either all records in a table or a single record with specified data
Method Syntaxupdate [ thing, 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 | ||
---|---|---|---|
thing required | The thing (Table or Record ID) to update | ||
data optional | The content of the record |
Request{ "id": 1, "method": "update", "params": [ "person:8s0j0bbm3ngrd5c9bx53", { "name": "John Doe" } ] }
Response{ "id": 1, "result": { "id": "person:8s0j0bbm3ngrd5c9bx53", "name": "John Doe" } }
upsert
Method Syntaxupsert [ thing, data ]
Parameter | Description | ||
---|---|---|---|
thing required | The thing (Table or Record ID) to upsert | ||
data optional | The content of the record |
Request{ "id": 1, "method": "upsert", "params": [ "person:12s0j0bbm3ngrd5c9bx53", { "name": "John Doe", "job": "Software developer", } ] }
Response{ "id": 1, "result": { "id": "person:12s0j0bbm3ngrd5c9bx53", "name": "John Doe", "job": "Software developer" } }
relate
Available since: v1.5.0
This method relates two records with a specified relation
Method Syntaxrelate [ in, relation, out, data? ]
Parameter | Description | ||
---|---|---|---|
in required | The record to relate to | ||
relation required | The relation table | ||
out required | The record to relate from | ||
data optional | The content of the record |
Request{ "id": 1, "method": "relate", "params": [ "person:12s0j0bbm3ngrd5c9bx53", "knows", "person:8s0j0bbm3ngrd5c9bx53" ] }
Response{ "id": 1, "result": { "id": "knows:12s0j0bbm3ngrd5c9bx53:8s0j0bbm3ngrd5c9bx53", "in": "person:12s0j0bbm3ngrd5c9bx53", "out": "person:8s0j0bbm3ngrd5c9bx53" } }
Request{ "id": 2, "method": "relate", "params": [ "person:john_doe", // in "knows", // relation "person:jane_smith", // out { "since": "2020-01-01" } // data ] }
Response{ "id": 2, "result": { "id": "knows:person:john_doe:person:jane_smith", "in": "person:jane_smith", "out": "person:john_doe", "since": "2020-01-01" } }
merge
This method merges specified data into either all records in a table or a single record
Method Syntaxmerge [ thing, data ]
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 | ||
---|---|---|---|
thing required | The thing (Table or Record ID) to merge into | ||
data optional | The content of the record |
Request{ "id": 1, "method": "merge", "params": [ "person", { "active": true } ] }
Response{ "id": 1, "result": [ { "active": true, "id": "person:8s0j0bbm3ngrd5c9bx53", "name": "John Doe" }, { "active": true, "id": "person:s5fa6qp4p8ey9k5j0m9z", "name": "Mary Doe" } ] }
patch
This method patches either all records in a table or a single record with specified patches
Method Syntaxpatch [ thing, patches, diff ]
NoteThis function patches the current document / record data with the specified JSON Patch data.
Parameter | Description | ||
---|---|---|---|
thing required | The thing (Table or Record ID) to patch | ||
patches required | An array of patches following the JSON Patch specification | ||
diff optional | A boolean representing if just a diff should be returned. |
Request{ "id": 1, "method": "patch", "params": [ "person", [ { "op": "replace", "path": "/last_updated", "value": "2023-06-16T08:34:25Z" } ] ] }
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" } ] ] }
delete
This method deletes either all records in a table or a single record
Method Syntaxdelete [ thing ]
Parameter | Description | ||
---|---|---|---|
thing required | The thing (Table or Record ID) to delete |
Request{ "id": 1, "method": "delete", "params": [ "person:8s0j0bbm3ngrd5c9bx53" ] }
Notice how the deleted record is being returned here
Response{ "id": 1, "result": { "active": true, "id": "person:8s0j0bbm3ngrd5c9bx53", "last_updated": "2023-06-16T08:34:25Z", "name": "John Doe" } }