Dart functions on Appwrite: base64 encode, HTTPS check and email slicer

Appwrite is an open-source backend-as-a-service: auth, databases, storage and serverless functions, either self-hosted or on Appwrite Cloud. Its Functions product runs your code in a managed runtime and gives each function an HTTP endpoint, so you don’t have to run a server for small utilities like the ones below.
The functions
Section titled “The functions”These are plain Dart and run anywhere. Each one became its own Appwrite function in the repo.
Base64 encode
Section titled “Base64 encode”import 'dart:convert';
String encode(String input) => base64Encode(utf8.encode(input));utf8.encode turns the string into bytes and base64Encode turns those bytes into a base64 string.
Both live in dart:convert.
Check for an HTTPS URL
Section titled “Check for an HTTPS URL”bool isHttpsUrl(String url) { final uri = Uri.tryParse(url); return uri != null && uri.scheme == 'https' && uri.host.isNotEmpty;}Uri.tryParse returns null instead of throwing on malformed input. Checking host matters because
https: on its own parses fine but isn’t a usable URL. This only checks the format; it doesn’t
check that the host exists or that its certificate is valid.
Email slicer
Section titled “Email slicer”Map<String, String> sliceEmail(String email) { final parts = email.split('@'); if (parts.length != 2 || parts[0].isEmpty || parts[1].isEmpty) { throw FormatException('Invalid email format', email); } return {'username': parts[0], 'domain': parts[1]};}
void main() { print(sliceEmail('example.user@example.com')); // {username: example.user, domain: example.com}}This splits on @ and rejects anything without exactly one. It isn’t a full email validator:
quoted local parts such as "a@b"@example.com are valid addresses that this rejects.
Running one as an Appwrite function
Section titled “Running one as an Appwrite function”An Appwrite Dart function is a package whose entrypoint (lib/main.dart in the starter template)
exports main(context). The handler reads the request from context.req and must return one of the
context.res helpers (text, json, binary, redirect, empty). Here is the encoder:
import 'dart:convert';
Future<dynamic> main(final context) async { final input = context.req.bodyText as String; if (input.isEmpty) { return context.res.json({'error': 'Send the text to encode as the request body.'}, 400); }
context.log('Encoding ${input.length} characters'); return context.res.json({ 'originalPayload': input, 'encodedPayload': base64Encode(utf8.encode(input)), });}context.req also has bodyJson, headers, query, path and method. Use context.log and
context.error instead of print; their output shows up in the execution logs in the Appwrite
Console. res.send still exists but is deprecated in favour of res.text.
These functions don’t call any Appwrite APIs, so they don’t need the SDK. If yours does, add
dart_appwrite (dart pub add dart_appwrite) and build the client from the values the runtime
provides:
import 'dart:io';import 'package:dart_appwrite/dart_appwrite.dart';
Future<dynamic> main(final context) async { final client = Client() .setEndpoint(Platform.environment['APPWRITE_FUNCTION_API_ENDPOINT'] ?? '') .setProject(Platform.environment['APPWRITE_FUNCTION_PROJECT_ID'] ?? '') .setKey(context.req.headers['x-appwrite-key'] ?? ''); final users = Users(client);
final result = await users.list(); return context.res.json({'totalUsers': result.total});}The x-appwrite-key header carries a short-lived API key that Appwrite issues for the execution,
scoped to the permissions you grant the function, so you don’t need to paste a long-lived key into
your code or variables.
Deploying
Section titled “Deploying”With the Appwrite CLI:
appwrite init functions # pick the Dart runtime; generates a starter function# replace lib/main.dart with your codeappwrite push functionsWithout the CLI, package the function folder with
tar --exclude code.tar.gz -czf code.tar.gz ., then upload it in the Console under the function’s
Create deployment > Manual tab and set the entrypoint (lib/main.dart).
Once deployed, you can run the function from the Console’s Execute button or call its domain
with an HTTP request; the request body becomes context.req.bodyText.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)