Oshi Gupta
Posted on October 28, 2021
Appwrite is an end-to-end backend server that is aiming to abstract the complexity of common, complex, and repetitive tasks required for building a modern app.
Appwrite is both cross-platform and technology agnostic, meaning it can run on any operating system, coding language, framework, or platform. Although Appwrite can easily fit the definition of serverless technology, it's designed to run well in multiple configurations.
It provides various libraries for major programming languages and platforms so you don't have to write code for interacting with our HTTP APIs from scratch.
Appwrite has divided SDKs into two categories
- Client SDKs
- Server SDKs
Client SDKs
- Web SDK
- Flutter SDK
- Apple SDK
- Android SDK
Server SDKs
- Kotlin SDK
- Dart SDK
- Swift SDK
- Python SDK
- Ruby SDK and etc
Here we will be seeing how to use kotlin in Appwrite functions
So what is Kotlin?
Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference.
Kotlin is designed to interoperate fully with Java, and the JVM version of Kotlin's standard library depends on the Java Class Library, but type inference allows its syntax to be more concise.
So how to write kotlin in appwrite function and for this install Kotlin SDK by following the below instructions
GRADLE
- Appwrite's Kotlin SDK is hosted on Maven Central. In order to fetch the Appwrite SDK, add this to your root-level build.gradle(.kts) file:
repositories {
mavenCentral()
}
- Next, add the dependency to your project's build.gradle(.kts) file:
dependencies {
implementation("io.appwrite:sdk-for-kotlin:")
}
MAVEN
- Add this to your pom.xml
<dependencies>
<dependency>
<groupId>io.appwrite</groupId>
<artifactId>sdk-for-kotlin</artifactId>
<version>0.1.1</version>
</dependency>
</dependencies>
Add the function
You can add a new function from your Appwrite project's dashboard. Access your Functions settings from your project's left navigation panel. Click the Add Function button and choose your function name and code runtime.
Init your SDK
- Initialize your SDK with your Appwrite server API endpoint and project ID which can be found in your project settings page and your new API secret Key project API keys section.
This can be found out when you install Appwrite according to the Operating System and access the Appwrite Console UI
import io.appwrite.Client
val client = Client()
client
.setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
.setProject('5df5acd0d48c2') // Your project ID
.setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
Make your First Request
import io.appwrite.services.Users
val users = Users(client);
val res = users.create(email = 'email@example.com', password = 'password');
println(res.body?.string())
As of now, we have seen how to set up the functions and Kotlin SDK.
Now, we will be seeing different operations which can be performed on these functions.
- Create functions : To create a new function with help of Kotlin
import io.appwrite.Client
import io.appwrite.services.Functions
suspend fun main() {
val client = Client(context)
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID
.setKey("919c2d18fb5d4...a2ae413da83346ad2") // Your secret API key
val functions = Functions(client)
val response = functions.create(
name = "[NAME]",
execute = listOf(),
runtime = "dotnet-5.0",
)
val json = response.body?.string()
}
- List functions : To get a list of all the project's functions. You can use the query params to filter your results.
import io.appwrite.Client
import io.appwrite.services.Functions
suspend fun main() {
val client = Client(context)
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint .setProject("5df5acd0d48c2") // Your project ID .setKey("919c2d18fb5d4...a2ae413da83346ad2") // Your secret API key
val functions = Functions(client)
val response = functions.list( )
val json = response.body?.string() }
- Get function : It will get a function by its unique ID.
val response = functions.get( functionId = "[FUNCTION_ID]" )
- Update function : It will update the function by its unique ID.
val response = functions.update(
functionId = "[FUNCTION_ID]",
name = "[NAME]",
execute = listOf(),
)
- Delete function : It will delete a function by its unique ID.
val response = functions.delete( functionId = "[FUNCTION_ID]" )
- Create execution : Trigger a function execution. The returned object will return you the current execution status. You can ping the Get Execution endpoint to get updates on the current execution status. Once this endpoint is called, your function execution process will start asynchronously.
val response = functions.createExecution( functionId = "[FUNCTION_ID]", )
So this is the basic idea of how to create functions in the Appwrite using the Kotlin SDK.
For more you can always refer to appwrite docs
Thank You
Posted on October 28, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.