Learn how to setup your first PHP project powered by Appwrite.
Head to the Appwrite Console.
If this is your first time using Appwrite, create an account and create your first project.


Then, under Integrate with your server, add an API Key with the following scopes.


Category | Required scopes | Purpose |
Database | databases.write | Allows API key to create, update, and delete databases. |
collections.write | Allows API key to create, update, and delete collections. | |
attributes.write | Allows API key to create, update, and delete attributes. | |
documents.read | Allows API key to read documents. | |
documents.write | Allows API key to create, update, and delete documents. |
Other scopes are optional.
Create a PHP CLI application.
mkdir my-app
cd my-app
composer init
Install the PHP Appwrite SDK.
composer require appwrite/appwrite:15.0.0
Find your project ID in the Settings page. Also, click on the View API Keys button to find the API key that was created earlier.


Create a new file index.php
and initialize the Appwrite Client. Replace <PROJECT_ID>
with your project ID and <YOUR_API_KEY>
with your API key.
<?php
require_once 'vendor/autoload.php';
use Appwrite\Client;
use Appwrite\Services\Databases;
use Appwrite\ID;
$client = new Client();
$client
->setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
->setProject('<PROJECT_ID>')
->setKey('<YOUR_API_KEY>');
Once the Appwrite Client is initialized, create a function to configure a todo collection.
$databases = new Databases($client);
function prepareDatabase($databases) {
$todoDatabase = $databases->create(
databaseId: ID::unique(),
name: 'TodosDB'
);
$todoCollection = $databases->createCollection(
databaseId: $todoDatabase['$id'],
collectionId: ID::unique(),
name: 'Todos'
);
$databases->createStringAttribute(
databaseId: $todoDatabase['$id'],
collectionId: $todoCollection['$id'],
key: 'title',
size: 255,
required: true
);
$databases->createStringAttribute(
databaseId: $todoDatabase['$id'],
collectionId: $todoCollection['$id'],
key: 'description',
size: 255,
required: false,
);
$databases->createBooleanAttribute(
databaseId: $todoDatabase['$id'],
collectionId: $todoCollection['$id'],
key: 'isComplete',
required: true
);
return [$todoDatabase, $todoCollection];
}
Create a function to add some mock data into your new collection.
function seedDatabase($databases, $todoDatabase, $todoCollection) {
$testTodo1 = [
'title' => 'Buy apples',
'description' => 'At least 2KGs',
'isComplete' => true
];
$testTodo2 = [
'title' => 'Wash the apples',
'isComplete' => true
];
$testTodo3 = [
'title' => 'Cut the apples',
'description' => 'Don\'t forget to pack them in a box',
'isComplete' => false
];
$databases->createDocument(
$todoDatabase['$id'],
$todoCollection['$id'],
ID::unique(),
$testTodo1
);
$databases->createDocument(
$todoDatabase['$id'],
$todoCollection['$id'],
ID::unique(),
$testTodo2
);
$databases->createDocument(
$todoDatabase['$id'],
$todoCollection['$id'],
ID::unique(),
$testTodo3
);
}
Create a function to retrieve the mock todo data and a function to execute the requests in order. Run the functions to by calling runAllTasks();
.
function getTodos($databases, $todoDatabase, $todoCollection) {
$todos = $databases->listDocuments(
$todoDatabase['$id'],
$todoCollection['$id']
);
foreach ($todos['documents'] as $todo) {
echo "Title: {$todo['title']}\n" .
"Description: {$todo['description']}\n" .
"Is Todo Complete: {$todo['isComplete']}\n\n";
}
}
function runAllTasks($databases) {
[$todoDatabase, $todoCollection] = prepareDatabase($databases);
seedDatabase($databases, $todoDatabase, $todoCollection);
getTodos($databases, $todoDatabase, $todoCollection);
}
runAllTasks($databases);
Run your project with php src/index.php
and view the response in your console.