Version 0.6.1  
Docs

Database API

Server integration with     code examples.


The Database service allows you to create structured collections of documents, query and filter lists of documents, and manage an advanced set of read and write access.

All the data in the database service is stored in structured JSON documents. The Appwrite database service also allows you to nest child documents in documents and use advanced filters to both search and query the database just like you would with a classic graph database.

Each database document structure in your project is defined using the Appwrite collection rules. The collections rules help you ensure all your user-submitted data is validated and stored according to the collection structure.

Using Appwrite permissions architecture, you can assign read or write access to each document in your project for either a specific user, team, user role, or even grant it with public access. You can learn more about how Appwrite handles permissions and access control.

Create Collection

POST/database/collections

Create a new Collection.

Authentication

To access this route, init your SDK with your project unique ID and API Key secret token. Make sure your API Key is granted with access to the "collections.write" permission scope.

Arguments

Name Type Description
name required string

Collection name.

read required array

An array of strings with read permissions. By default no user is granted with any read permissions. learn more about permissions and get a full list of available permissions.

write required array

An array of strings with write permissions. By default no user is granted with any write permissions. learn more about permissions and get a full list of available permissions.

rules required array

Array of rule objects. Each rule define a collection field name, data type and validation.

Example Request
  • const sdk = require('node-appwrite');
    
    // Init SDK
    let client = new sdk.Client();
    
    let database = new sdk.Database(client);
    
    client
        .setProject('5df5acd0d48c2') // Your project ID
        .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    let promise = database.createCollection('[NAME]', [], [], []);
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        ->setProject('5df5acd0d48c2') // Your project ID
        ->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    $database = new Database($client);
    
    $result = $database->createCollection('[NAME]', [], [], []);
  • from appwrite.client import Client
    from appwrite.services.database import Database
    
    client = Client()
    
    (client
      .set_project('5df5acd0d48c2') # Your project ID
      .set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
    )
    
    database = Database(client)
    
    result = database.create_collection('[NAME]', {}, {}, {})
    

List Collections

GET/database/collections

Get a list of all the user collections. You can use the query params to filter your results. On admin mode, this endpoint will return a list of all of the project collections. Learn more about different API modes.

Authentication

To access this route, init your SDK with your project unique ID and API Key secret token. Make sure your API Key is granted with access to the "collections.read" permission scope.

Arguments

Name Type Description
search optional string

Search term to filter your list results.

limit optional integer

Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.

offset optional integer

Results offset. The default value is 0. Use this param to manage pagination.

orderType optional string

Order result by ASC or DESC order.

Example Request
  • const sdk = require('node-appwrite');
    
    // Init SDK
    let client = new sdk.Client();
    
    let database = new sdk.Database(client);
    
    client
        .setProject('5df5acd0d48c2') // Your project ID
        .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    let promise = database.listCollections();
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        ->setProject('5df5acd0d48c2') // Your project ID
        ->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    $database = new Database($client);
    
    $result = $database->listCollections();
  • from appwrite.client import Client
    from appwrite.services.database import Database
    
    client = Client()
    
    (client
      .set_project('5df5acd0d48c2') # Your project ID
      .set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
    )
    
    database = Database(client)
    
    result = database.list_collections()
    

Get Collection

GET/database/collections/{collectionId}

Get collection by its unique ID. This endpoint response returns a JSON object with the collection metadata.

Authentication

To access this route, init your SDK with your project unique ID and API Key secret token. Make sure your API Key is granted with access to the "collections.read" permission scope.

Arguments

Name Type Description
collectionId required string

Collection unique ID.

Example Request
  • const sdk = require('node-appwrite');
    
    // Init SDK
    let client = new sdk.Client();
    
    let database = new sdk.Database(client);
    
    client
        .setProject('5df5acd0d48c2') // Your project ID
        .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    let promise = database.getCollection('[COLLECTION_ID]');
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        ->setProject('5df5acd0d48c2') // Your project ID
        ->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    $database = new Database($client);
    
    $result = $database->getCollection('[COLLECTION_ID]');
  • from appwrite.client import Client
    from appwrite.services.database import Database
    
    client = Client()
    
    (client
      .set_project('5df5acd0d48c2') # Your project ID
      .set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
    )
    
    database = Database(client)
    
    result = database.get_collection('[COLLECTION_ID]')
    

Get Collection Logs

GET/database/collections/{collectionId}/logs

Authentication

To access this route, init your SDK with your project unique ID and API Key secret token. Make sure your API Key is granted with access to the "collections.read" permission scope.

Arguments

Name Type Description
collectionId required string

Collection unique ID.

Example Request
  • const sdk = require('node-appwrite');
    
    // Init SDK
    let client = new sdk.Client();
    
    let database = new sdk.Database(client);
    
    client
        .setProject('5df5acd0d48c2') // Your project ID
        .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    let promise = database.getCollectionLogs('[COLLECTION_ID]');
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        ->setProject('5df5acd0d48c2') // Your project ID
        ->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    $database = new Database($client);
    
    $result = $database->getCollectionLogs('[COLLECTION_ID]');
  • from appwrite.client import Client
    from appwrite.services.database import Database
    
    client = Client()
    
    (client
      .set_project('5df5acd0d48c2') # Your project ID
      .set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
    )
    
    database = Database(client)
    
    result = database.get_collection_logs('[COLLECTION_ID]')
    

Update Collection

PUT/database/collections/{collectionId}

Update collection by its unique ID.

Authentication

To access this route, init your SDK with your project unique ID and API Key secret token. Make sure your API Key is granted with access to the "collections.write" permission scope.

Arguments

Name Type Description
collectionId required string

Collection unique ID.

name required string

Collection name.

read required array

An array of strings with read permissions. By default no user is granted with any read permissions. [learn more about permissions(/docs/permissions) and get a full list of available permissions.

write required array

An array of strings with write permissions. By default no user is granted with any write permissions. learn more about permissions and get a full list of available permissions.

rules optional array

Array of rule objects. Each rule define a collection field name, data type and validation.

Example Request
  • const sdk = require('node-appwrite');
    
    // Init SDK
    let client = new sdk.Client();
    
    let database = new sdk.Database(client);
    
    client
        .setProject('5df5acd0d48c2') // Your project ID
        .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    let promise = database.updateCollection('[COLLECTION_ID]', '[NAME]', [], []);
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        ->setProject('5df5acd0d48c2') // Your project ID
        ->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    $database = new Database($client);
    
    $result = $database->updateCollection('[COLLECTION_ID]', '[NAME]', [], []);
  • from appwrite.client import Client
    from appwrite.services.database import Database
    
    client = Client()
    
    (client
      .set_project('5df5acd0d48c2') # Your project ID
      .set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
    )
    
    database = Database(client)
    
    result = database.update_collection('[COLLECTION_ID]', '[NAME]', {}, {})
    

Delete Collection

DELETE/database/collections/{collectionId}

Delete a collection by its unique ID. Only users with write permissions have access to delete this resource.

Authentication

To access this route, init your SDK with your project unique ID and API Key secret token. Make sure your API Key is granted with access to the "collections.write" permission scope.

Arguments

Name Type Description
collectionId required string

Collection unique ID.

Example Request
  • const sdk = require('node-appwrite');
    
    // Init SDK
    let client = new sdk.Client();
    
    let database = new sdk.Database(client);
    
    client
        .setProject('5df5acd0d48c2') // Your project ID
        .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    let promise = database.deleteCollection('[COLLECTION_ID]');
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        ->setProject('5df5acd0d48c2') // Your project ID
        ->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    $database = new Database($client);
    
    $result = $database->deleteCollection('[COLLECTION_ID]');
  • from appwrite.client import Client
    from appwrite.services.database import Database
    
    client = Client()
    
    (client
      .set_project('5df5acd0d48c2') # Your project ID
      .set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
    )
    
    database = Database(client)
    
    result = database.delete_collection('[COLLECTION_ID]')
    

Create Document

POST/database/collections/{collectionId}/documents

Create a new Document.

Authentication

To access this route, init your SDK with your project unique ID and API Key secret token. Make sure your API Key is granted with access to the "documents.write" permission scope.

Arguments

Name Type Description
collectionId required string

Collection unique ID. You can create a new collection with validation rules using the Database service server integration.

data required object

Document data as JSON object.

read required array

An array of strings with read permissions. By default no user is granted with any read permissions. learn more about permissions and get a full list of available permissions.

write required array

An array of strings with write permissions. By default no user is granted with any write permissions. learn more about permissions and get a full list of available permissions.

parentDocument optional string

Parent document unique ID. Use when you want your new document to be a child of a parent document.

parentProperty optional string

Parent document property name. Use when you want your new document to be a child of a parent document.

parentPropertyType optional string

Parent document property connection type. You can set this value to assign, append or prepend, default value is assign. Use when you want your new document to be a child of a parent document.

Example Request
  • const sdk = require('node-appwrite');
    
    // Init SDK
    let client = new sdk.Client();
    
    let database = new sdk.Database(client);
    
    client
        .setProject('5df5acd0d48c2') // Your project ID
        .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    let promise = database.createDocument('[COLLECTION_ID]', {}, [], []);
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        ->setProject('5df5acd0d48c2') // Your project ID
        ->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    $database = new Database($client);
    
    $result = $database->createDocument('[COLLECTION_ID]', [], [], []);
  • from appwrite.client import Client
    from appwrite.services.database import Database
    
    client = Client()
    
    (client
      .set_project('5df5acd0d48c2') # Your project ID
      .set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
    )
    
    database = Database(client)
    
    result = database.create_document('[COLLECTION_ID]', {}, {}, {})
    

List Documents

GET/database/collections/{collectionId}/documents

Get a list of all the user documents. You can use the query params to filter your results. On admin mode, this endpoint will return a list of all of the project documents. Learn more about different API modes.

Authentication

To access this route, init your SDK with your project unique ID and API Key secret token. Make sure your API Key is granted with access to the "documents.read" permission scope.

Arguments

Name Type Description
collectionId required string

Collection unique ID. You can create a new collection with validation rules using the Database service server integration.

filters optional array

Array of filter strings. Each filter is constructed from a key name, comparison operator (=, !=, >, <, <=, >=) and a value. You can also use a dot (.) separator in attribute names to filter by child document attributes. Examples: 'name=John Doe' or 'category.$id>=5bed2d152c362'.

offset optional integer

Offset value. Use this value to manage pagination.

limit optional integer

Maximum number of documents to return in response. Use this value to manage pagination.

orderField optional string

Document field that results will be sorted by.

orderType optional string

Order direction. Possible values are DESC for descending order, or ASC for ascending order.

orderCast optional string

Order field type casting. Possible values are int, string, date, time or datetime. The database will attempt to cast the order field to the value you pass here. The default value is a string.

search optional string

Search query. Enter any free text search. The database will try to find a match against all document attributes and children.

first optional integer

Return only the first document. Pass 1 for true or 0 for false. The default value is 0.

last optional integer

Return only the last document. Pass 1 for true or 0 for false. The default value is 0.

Example Request
  • const sdk = require('node-appwrite');
    
    // Init SDK
    let client = new sdk.Client();
    
    let database = new sdk.Database(client);
    
    client
        .setProject('5df5acd0d48c2') // Your project ID
        .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    let promise = database.listDocuments('[COLLECTION_ID]');
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        ->setProject('5df5acd0d48c2') // Your project ID
        ->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    $database = new Database($client);
    
    $result = $database->listDocuments('[COLLECTION_ID]');
  • from appwrite.client import Client
    from appwrite.services.database import Database
    
    client = Client()
    
    (client
      .set_project('5df5acd0d48c2') # Your project ID
      .set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
    )
    
    database = Database(client)
    
    result = database.list_documents('[COLLECTION_ID]')
    

Get Document

GET/database/collections/{collectionId}/documents/{documentId}

Get document by its unique ID. This endpoint response returns a JSON object with the document data.

Authentication

To access this route, init your SDK with your project unique ID and API Key secret token. Make sure your API Key is granted with access to the "documents.read" permission scope.

Arguments

Name Type Description
collectionId required string

Collection unique ID. You can create a new collection with validation rules using the Database service server integration.

documentId required string

Document unique ID.

Example Request
  • const sdk = require('node-appwrite');
    
    // Init SDK
    let client = new sdk.Client();
    
    let database = new sdk.Database(client);
    
    client
        .setProject('5df5acd0d48c2') // Your project ID
        .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    let promise = database.getDocument('[COLLECTION_ID]', '[DOCUMENT_ID]');
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        ->setProject('5df5acd0d48c2') // Your project ID
        ->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    $database = new Database($client);
    
    $result = $database->getDocument('[COLLECTION_ID]', '[DOCUMENT_ID]');
  • from appwrite.client import Client
    from appwrite.services.database import Database
    
    client = Client()
    
    (client
      .set_project('5df5acd0d48c2') # Your project ID
      .set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
    )
    
    database = Database(client)
    
    result = database.get_document('[COLLECTION_ID]', '[DOCUMENT_ID]')
    

Update Document

PATCH/database/collections/{collectionId}/documents/{documentId}

Authentication

To access this route, init your SDK with your project unique ID and API Key secret token. Make sure your API Key is granted with access to the "documents.write" permission scope.

Arguments

Name Type Description
collectionId required string

Collection unique ID. You can create a new collection with validation rules using the Database service server integration.

documentId required string

Document unique ID.

data required object

Document data as JSON object.

read required array

An array of strings with read permissions. By default no user is granted with any read permissions. learn more about permissions and get a full list of available permissions.

write required array

An array of strings with write permissions. By default no user is granted with any write permissions. learn more about permissions and get a full list of available permissions.

Example Request
  • const sdk = require('node-appwrite');
    
    // Init SDK
    let client = new sdk.Client();
    
    let database = new sdk.Database(client);
    
    client
        .setProject('5df5acd0d48c2') // Your project ID
        .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    let promise = database.updateDocument('[COLLECTION_ID]', '[DOCUMENT_ID]', {}, [], []);
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        ->setProject('5df5acd0d48c2') // Your project ID
        ->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    $database = new Database($client);
    
    $result = $database->updateDocument('[COLLECTION_ID]', '[DOCUMENT_ID]', [], [], []);
  • from appwrite.client import Client
    from appwrite.services.database import Database
    
    client = Client()
    
    (client
      .set_project('5df5acd0d48c2') # Your project ID
      .set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
    )
    
    database = Database(client)
    
    result = database.update_document('[COLLECTION_ID]', '[DOCUMENT_ID]', {}, {}, {})
    

Delete Document

DELETE/database/collections/{collectionId}/documents/{documentId}

Delete document by its unique ID. This endpoint deletes only the parent documents, his attributes and relations to other documents. Child documents will not be deleted.

Authentication

To access this route, init your SDK with your project unique ID and API Key secret token. Make sure your API Key is granted with access to the "documents.write" permission scope.

Arguments

Name Type Description
collectionId required string

Collection unique ID. You can create a new collection with validation rules using the Database service server integration.

documentId required string

Document unique ID.

Example Request
  • const sdk = require('node-appwrite');
    
    // Init SDK
    let client = new sdk.Client();
    
    let database = new sdk.Database(client);
    
    client
        .setProject('5df5acd0d48c2') // Your project ID
        .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    let promise = database.deleteDocument('[COLLECTION_ID]', '[DOCUMENT_ID]');
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        ->setProject('5df5acd0d48c2') // Your project ID
        ->setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
    ;
    
    $database = new Database($client);
    
    $result = $database->deleteDocument('[COLLECTION_ID]', '[DOCUMENT_ID]');
  • from appwrite.client import Client
    from appwrite.services.database import Database
    
    client = Client()
    
    (client
      .set_project('5df5acd0d48c2') # Your project ID
      .set_key('919c2d18fb5d4...a2ae413da83346ad2') # Your secret API key
    )
    
    database = Database(client)
    
    result = database.delete_document('[COLLECTION_ID]', '[DOCUMENT_ID]')