HTTP & Rest
The HTTP endpoints exposed by SurrealDB instances provide a simple way to interact with the database over a traditional RESTful interface. This includes selecting and modifying one or more records, executing custom SurrealQL queries, and importing and exporting data.
The endpoints are designed to be simple and easy to use in stateless environments, making them ideal for lightweight applications where a persistent database connection is not required.
Setup
The surreal start command without any arguments is all that is needed to start a server at the default http://localhost:8000 address. Many examples below assume the flags --user root and --pass secret to create a root user with the name root and password secret. The --unauthenticated flag can be used when experimenting to turn off authentication, effectively allowing root access by any and all connections.
The local database serving functionality on the Surrealist UI can also be used to start a server.
Querying via Postman
One convenient way to access these endpoints is via SurrealDB's Postman Collection. To do so, follow these steps:
Open Postman
Clone the SurrealDB Postman Collection
Select the appropriate HTTP method (
GET /health,DEL /key/:table, etc.).Enter the endpoint URL.
If the endpoint requires any parameters or a body, make sure to include those in your request.
Supported methods
You can use the HTTP endpoints to perform the following actions:
| Function | Description |
|---|---|
GET /status | Checks whether the database web server is running |
GET /health | Checks the status of the database server and storage engine |
GET /version | Returns the version of the SurrealDB database server |
POST /import | Imports data into a specific Namespace and Database |
POST /export | Exports all data for a specific Namespace and Database |
POST /signup | Signs-up as a record user using a specific record access method |
POST /signin | Signs-in as a root, namespace, database, or record user |
GET /key/:table | Selects all records in a table from the database |
POST /key/:table | Creates a record in a table in the database |
PUT /key/:table | Updates all records in a table in the database |
PATCH /key/:table | Modifies all records in a table in the database |
DELETE /key/:table | Deletes all records in a table from the database |
GET /key/:table/:id | Selects the specific record from the database |
POST /key/:table/:id | Creates the specific record in the database |
PUT /key/:table/:id | Updates the specified record in the database |
PATCH /key/:table/:id | Modifies the specified record in the database |
DELETE /key/:table/:id | Deletes the specified record from the database |
POST /sql | Allows custom SurrealQL queries |
POST /graphql | Allows custom GraphQL queries |
POST /ml/import | Import a SurrealML model into a specific Namespace and Database |
GET /ml/export/:name/:version | Export a SurrealML model from a specific Namespace and Database |
/api/:namespace/:database/:endpoint | Create a custom API endpoint for any number of HTTP methods (GET, POST, etc.) |
GET /status
This HTTP RESTful endpoint checks whether the database web server is running, returning a 200 status code.
Example usage
GET /health
This HTTP RESTful endpoint checks whether the database server and storage engine are running.
The endpoint returns a 200 status code on success and a 500 status code on failure.
GET /version
This HTTP RESTful endpoint returns the version of the SurrealDB database server.
Example usage
POST /import
This HTTP RESTful endpoint imports a set of SurrealQL queries into a specific namespace and database.
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, or database authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Example usage
Note
POST /export
This HTTP RESTful endpoint exports all data for a specific Namespace and Database.
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, or database authentication data |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Example usage
Note
POST /signin
This HTTP RESTful endpoint is used to access an existing account inside the SurrealDB database server.
Headers
| Header | Description |
|---|---|
Accept
| Sets the desired content-type of the response |
Data
| Data | Description |
|---|---|
ns
REQUIRED FOR DB & RECORD
| The namespace to sign in to this is required FOR DB & RECORD users |
db
REQUIRED FOR RECORD
| The database to sign in to required for RECORD users |
ac
REQUIRED FOR RECORD USER
| The record access method to use for signing in. required for RECORD users |
user
REQUIRED FOR ROOT, NS & DB
| The username of the database user required for ROOT, NS & DB users |
pass
REQUIRED FOR ROOT, NS & DB
| The password of the database user required for ROOT, NS & DB users |
Important
Example with a Record user
The following example will work as long as as an access method has been defined and a record user has been signed up using the /signup endpoint.
Example with Root user
Example with Namespace user
To create the namespace user needed for the following query, use the following command.
Once the user has been created, use this command to sign in.
Example usage via Postman
After you have defined the users permissions for the record user, you can use the POST /signin endpoint to sign in as a user.
Using the user credentials created add the following to the request body:
POST /signup
This HTTP RESTful endpoint is used to create an account inside the SurrealDB database server.
Header
| Header | Description |
|---|---|
Accept
| Sets the desired content-type of the response |
Data
| Data | Description |
|---|---|
ns
|
The namespace to sign up to. This data is REQUIRED FOR DB & RECORD
|
db
|
The database to sign up to. This data is REQUIRED FOR RECORD
|
access
|
The record access method to use for signing up. This data is REQUIRED FOR RECORD
|
user
|
The username of the database user. This data is REQUIRED FOR ROOT, NS & DB
|
pass
|
The password of the database user. This data is REQUIRED FOR ROOT, NS & DB
|
Example usage
The above example will only work if a record access method has already been set up.
Setting up a record access method
Before you sign up a new record user, you must first define a record access method for the user. The following curl command will do so on the command line using the POST /sql endpoint.
To do the same using Postman, use the following steps:
Navigate to the
POST /sqlendpoint in Postman.Enter the following query in the body of the request:
The above query defines a record access method called account that allows users to sign up and sign in. The access method also defines the session duration to be 24 hours.
Click
Sendto send the request to the SurrealDB database server.Navigate to the
POST /signupendpoint in Postman.Enter the following query in the body of the request:
In the header of the request, set the following key-value pairs:
Accept: application/jsonnamespace:
testdatabase:
testaccess:
account
Click
Sendto send the request to the SurrealDB database server. You will receive the following response.
GET /key/:table
This HTTP RESTful endpoint selects all records in a specific table in the database.
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Translated query
Example usage
POST /key/:table
This HTTP RESTful endpoint creates a record in a specific table in the database.
Note
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Translated query
Example usage
PUT /key/:table
This HTTP RESTful endpoint updates all records in a specific table in the database.
Note
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Translated query
Example usage
To use this example, first create a record using the POST endpoint:
Then use this PUT endpoint to modify the existing record.
PATCH /key/:table
This HTTP RESTful endpoint modifies all records in a specific table in the database.
Note
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Translated query
Example usage
To use this example, first create a record using the POST endpoint:
Then use this PATCH endpoint to modify the existing records.
DELETE /key/:table
This HTTP RESTful endpoint deletes all records from the specified table in the database.
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Translated query
Example usage
To use this example, first create a record using the POST endpoint:
Then use this DELETE endpoint to delete and return the records that were just removed.
GET /key/:table/:id
This HTTP RESTful endpoint selects a specific record from the database.
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Translated query
Example usage
POST /key/:table/:id
This HTTP RESTful endpoint creates a specific record in a table in the database.
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Translated query
Example usage
PUT /key/:table/:id
This HTTP RESTful endpoint updates a specific record in a table in the database.
Note
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Translated query
PATCH /key/:table/:id
This HTTP RESTful endpoint modifies a specific record in a table in the database.
Note
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Translated query
Example usage
To use this example, first create a record using the POST endpoint:
Example usage
To use this example, first create a record using the POST endpoint:
Then use this PATCH endpoint to modify the existing record.
DELETE /key/:table/:id
This HTTP RESTful endpoint deletes a single specific record from the database.
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Translated query
Example usage
To use this example, first create a record using the POST endpoint:
Then use this DELETE endpoint to delete and return the record that was just removed.
POST /sql
The SQL endpoint enables use of SurrealQL queries.
Note
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Parameters
Query parameters can be provided via URL query parameters. These parameters will securely replace any parameters that are present in the query. This practise is known as prepared statements or parameterised queries, and should be used whenever untrusted inputs are included in a query to prevent injection attacks.
Example usage
Note
Usage in importing data
As of SurrealDB 3.0.4, imports via the surreal import and /import HTTP endpoint require the automatically generated OPTION IMPORT line to be present in order to disable events, live queries, field processing, and result output for optimal import performance. If side effects are desired when importing data, remove the line and use this endpoint instead.
POST /graphql
The GraphQL endpoint enables use of GraphQL queries to interact with your data.
Note
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Accept
| Sets the desired content-type of the response |
Surreal-NS
| Sets the selected Namespace for queries |
Surreal-DB
| Sets the selected Database for queries |
Example usage
Note
First, use the /sql endpoint to send in a DEFINE CONFIG statement to set the database up to use GraphQL.
With that done, a GraphQL query can now be performed.
POST /ml/import
This HTTP RESTful endpoint imports a SurrealML machine learning model into a specific Namespace and Database. It expects the file to be a SurrealML file packaged in the .surml file format. As machine learning files can be large, the endpoint expects a chunked HTTP request.
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, database, or record authentication data |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Example usage
Note
Usage in Python
When using Python, the surreaml package can be used to upload the model with the following code:
GET /ml/export/:name/:version
This HTTP RESTful endpoint exports a SurrealML machine learning model from a specific Namespace and Database. The output file with be a SurrealML file packaged in the .surml file format. As machine learning files can be large, the endpoint outputs a chunked HTTP response.
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, or database authentication data |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Example usage
Note
Custom endpoint at /api/:ns/:db/:endpoint
A custom endpoint can be set using a DEFINE API statement. The possible HTTP methods (GET, PUT, etc.) are set using the statement itself. The path begins with /api, continues with the namespace and database, and ends with a custom endpoint that can include both static and dynamic path segments.
Headers
| Header | Description |
|---|---|
Authorization
OPTIONAL
| Sets the root, namespace, or database authentication data |
Surreal-NS
| Sets the selected Namespace for queries. |
Surreal-DB
| Sets the selected Database for queries. |
Example usage
To begin, start a server with the surreal start command.
A custom endpoint can first be set up using a DEFINE API statement via the /sql endpoint.
Once this is set up, a simple curl command to the endpoint will suffice to see the response.