Learn how to set up your first Go project powered by Appwrite.
Create project
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 Go project
Create a go application.
mkdir my-app
cd my-app
go mod init go-appwrite/main
Install Appwrite
Install the Go Appwrite SDK.
go get github.com/appwrite/sdk-for-go
Import Appwrite
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 called app.go, initialize a function, and initialize the Appwrite Client. Replace <PROJECT_ID> with your project ID and <YOUR_API_KEY> with your API key. Import the Appwrite dependencies for appwrite, client, databases, and models.
package main
import (
"github.com/appwrite/sdk-for-go/appwrite"
"github.com/appwrite/sdk-for-go/client"
"github.com/appwrite/sdk-for-go/databases"
"github.com/appwrite/sdk-for-go/models"
)
var (
appwriteClient client.Client
todoDatabase *models.Database
todoCollection *models.Collection
appwriteDatabases *databases.Databases
)
func main() {
appwriteClient = appwrite.NewClient(
appwrite.WithProject("<PROJECT_KEY>"),
appwrite.WithKey("<API_KEY>"),
)
}
Initialize database
Once the Appwrite Client is initialized, create a function to configure a todo collection. Import the id Appwrite dependency by adding "github.com/appwrite/sdk-for-go/id" to the imported dependencies list.
func prepareDatabase() {
appwriteDatabases = appwrite.NewDatabases(appwriteClient)
todoDatabase, _ = appwriteDatabases.Create(
id.Unique(),
"TodosDB",
)
todoCollection, _ = appwriteDatabases.CreateCollection(
todoDatabase.Id,
id.Unique(),
"Todos",
)
appwriteDatabases.CreateStringAttribute(
todoDatabase.Id,
todoCollection.Id,
"title",
255,
true,
)
appwriteDatabases.CreateStringAttribute(
todoDatabase.Id,
todoCollection.Id,
"description",
255,
false,
)
appwriteDatabases.CreateBooleanAttribute(
todoDatabase.Id,
todoCollection.Id,
"isComplete",
true,
)
}
Add documents
Create a function to add some mock data to your new collection.
func seedDatabase() {
testTodo1 := map[string]interface{}{
"title": "Buy apples",
"description": "At least 2KGs",
"isComplete": true,
}
testTodo2 := map[string]interface{}{
"title": "Wash the apples",
"isComplete": true,
}
testTodo3 := map[string]interface{}{
"title": "Cut the apples",
"description": "Don't forget to pack them in a box",
"isComplete": false,
}
appwriteDatabases.CreateDocument(
todoDatabase.Id,
todoCollection.Id,
id.Unique(),
testTodo1,
)
appwriteDatabases.CreateDocument(
todoDatabase.Id,
todoCollection.Id,
id.Unique(),
testTodo2,
)
appwriteDatabases.CreateDocument(
todoDatabase.Id,
todoCollection.Id,
id.Unique(),
testTodo3,
)
}
Retrieve documents
Create a function to retrieve the mock todo data.
type Todo struct {
Title string `json:"title"`
Description string `json:"description"`
IsComplete bool `json:"isComplete"`
}
type TodoList struct {
*models.DocumentList
Documents []Todo `json:"documents"`
}
func getTodos() {
todoResponse, _ := appwriteDatabases.ListDocuments(
todoDatabase.Id,
todoCollection.Id,
)
var todos TodoList
todoResponse.Decode(&todos)
for _, todo := range todos.Documents {
fmt.Printf("Title: %s\nDescription: %s\nIs Todo Complete: %t\n\n", todo.Title, todo.Description, todo.IsComplete)
}
}
Make sure to update main() with the functions you created. Your main() function should look something like this:
package main
import (
"fmt"
"github.com/appwrite/sdk-for-go/appwrite"
"github.com/appwrite/sdk-for-go/client"
"github.com/appwrite/sdk-for-go/databases"
"github.com/appwrite/sdk-for-go/id"
"github.com/appwrite/sdk-for-go/models"
)
var (
appwriteClient client.Client
todoDatabase *models.Database
todoCollection *models.Collection
appwriteDatabases *databases.Databases
)
func main() {
appwriteClient = appwrite.NewClient(
appwrite.WithProject("<PROJECT_KEY>"),
appwrite.WithKey("<API_KEY>"),
)
prepareDatabase()
seedDatabase()
getTodos()
}
All set
Run your project with go run . and view the response in your console.