Skip to content

Instantly share code, notes, and snippets.

@brandonroberts
Created February 28, 2022 19:34
Show Gist options
  • Save brandonroberts/94918e4f8fe90f3880e8561ef489b787 to your computer and use it in GitHub Desktop.
Save brandonroberts/94918e4f8fe90f3880e8561ef489b787 to your computer and use it in GitHub Desktop.
Appwrite Function
const sdk = require("node-appwrite");
/*
'req' variable has:
'headers' - object with request headers
'payload' - object with request body data
'env' - object with environment variables
'res' variable has:
'send(text, status)' - function to return text response. Status code defaults to 200
'json(obj, status)' - function to return JSON response. Status code defaults to 200
If an error is thrown, a response with code 500 will be returned.
*/
module.exports = async function (req, res) {
const client = new sdk.Client();
// You can remove services you don't use
let account = new sdk.Account(client);
let avatars = new sdk.Avatars(client);
let database = new sdk.Database(client);
let functions = new sdk.Functions(client);
let health = new sdk.Health(client);
let locale = new sdk.Locale(client);
let storage = new sdk.Storage(client);
let teams = new sdk.Teams(client);
let users = new sdk.Users(client);
if (
!req.env['APPWRITE_FUNCTION_ENDPOINT'] ||
!req.env['APPWRITE_FUNCTION_API_KEY']
) {
console.warn("Environment variables are not set. Function cannot use Appwrite SDK.");
} else {
client
.setEndpoint(req.env['APPWRITE_FUNCTION_ENDPOINT'])
.setProject(req.env['APPWRITE_FUNCTION_PROJECT_ID'])
.setKey(req.env['APPWRITE_FUNCTION_API_KEY'])
.setSelfSigned(true);
}
res.json({
areDevelopersAwesome: true,
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment