Quickstart
The Python SDK for SurrealDB makes it straightforward to connect to your instance and start querying data. This guide walks you through connecting, authenticating, and performing basic operations.
1. Install the SDK
Follow the installation guide to install the SDK as a dependency in your project. Once installed, import the SDK to start using it.
The Surreal and AsyncSurreal factory functions accept a connection URL and return the appropriate connection class based on the protocol.
2. Connect to SurrealDB
You can use the .connect() method to open the connection, then .use() to select a namespace and database, and .signin() to authenticate.
Supported connection protocols include:
WebSocket (
ws://,wss://) for long-lived stateful connectionsHTTP (
http://,https://) for short-lived stateless connectionsEmbedded (
mem://,file://,surrealkv://) for in-process databases
You can also use a context manager to automatically close the connection when you are done.
3. Inserting data into SurrealDB
Once connected, you can use the .create() method to create records. Pass a table name or a RecordID as the first argument and the record data as the second.
4. Retrieving data from SurrealDB
Selecting records
The .select() method retrieves all records from a table, or a single record by its RecordID.
Running SurrealQL queries
For more advanced use cases, you can use the .query() method to run SurrealQL statements directly. Use the vars parameter to safely pass dynamic values.
5. Closing the connection
Always close the connection when you are done to release resources. If you use a context manager, this happens automatically.
What's next?
You have learned how to install the SDK, connect to SurrealDB, create records, and retrieve data. There is a lot more you can do with the SDK, including updating and deleting records, handling authentication, live queries, sessions, and transactions.
Connection management
Learn how to manage your database connections, including protocols and context managers.
Authentication
Read more about authentication and how to integrate it into your application.
Data manipulation
Learn how to create, read, update, and delete records using the SDK.
API Reference
Complete reference for all classes, methods, types, and errors.