The Deta SDK
The Deta library is the easiest way to store and retrieve data from your Deta Base and Deta Drive in a Collection. Currently, we support JavaScript (Node + Browser), Python 3 and Go.
You’ll first need to install and instantiate your SDK, before you can interact with Base or Drive.
Once you have, if you’re someone who learns by theory, see the reference documentation:
If you’re someone who learns by example, there are also extensive code examples.
Installing
go get github.com/deta/deta-go
pip install deta
pip install 'deta[async]'
yarn add deta
<script type="module"> import { Deta } from 'https://cdn.deta.space/js/deta@latest/deta.mjs';</script>
npm install deta
ℹ️ If you are using the Deta SDK within a Deta Micro, you must include
deta
in yourpackage.json
file to install the latest sdk version.
Instantiating
To start working with your Base or Drive, you need to import the Deta class and initialize it.
The SDK needs to be authenticated with Space to talk with a Base or Drive.
Once you’re authenticated, you can instantiate a subclass called Base with a database name of your choosing or Drive with a drive name of your choosing.
Deta Bases and Drives are created for you automatically when you start using them.
import ( "fmt"
"github.com/deta/deta-go/deta" "github.com/deta/deta-go/service/base")
func main() {
// initialize with project key // returns ErrBadProjectKey if project key is invalid // locally, set the project key in an env var called DETA_PROJECT_KEY d, err := deta.New() if err != nil { fmt.Println("failed to init new Deta instance:", err) return }
// initialize with base name // returns ErrBadBaseName if base name is invalid db, err := base.New(d, "base_name") if err != nil { fmt.Println("failed to init new Base instance:", err) return }}
const { Deta } = require('deta'); // import Deta
// Initializeconst deta = Deta();
// This how to connect to or create a database.const db = deta.Base('simple_db');
// This how to connect to or create a drive.const drive = deta.Drive('my_store');
// You can create as many as you want.const books = deta.Base('books');
from deta import Deta # Import Deta
# Initializedeta = Deta()
# This how to connect to or create a database.db = deta.Base("simple_db")
# This how to connect to or create a drive.db = deta.Drive("my_store")
# You can create as many as you want without additional charges.books = deta.Base("books")
from deta import Deta # Import Deta
# Initializedeta = Deta()
# This how to connect to or create a database.db = deta.AsyncBase("simple_db")
# This how to connect to or create a drive. (not async)db = deta.Drive("my_store")
# You can create as many as you want without additional charges.books = deta.AsyncBase("books")
<script type="module"> // import import { Base, Drive } from 'https://cdn.deta.space/js/deta@latest/deta.mjs'
// Connect to or create a Base const db = Base('my_base');
// Connect to or create a Drive const drive = Drive('my_drive');
// You can connect to or create as many as you want const books = Base('books');</script>
⚠️ Do not use the browser version of the SDK with your secret key hard coded. You are exposing your key. The browser SDK is strictly meant for use in Space Apps’ private routes.
Authentication
Authentication is managed for you when developing locally with the space dev
command,
when using the SDK inside a Micro,
or when using the SDK in a Space app running in the browser (the SDK will not authenticate on public micros or public routes).
Alternatively, you can authenticate with the Deta SDK by providing a Data Key in an environment variable called DETA_PROJECT_KEY
.
⚠️ Your Data Key is confidential and meant to be used by you. Anyone who has your Data Key can access your database. Please, do not share it, commit it in your code, or use it in the Browser.