Skip to content
Init is here / May 19 - 23
Back

What is the context parameter in a Appwrite function?

  • 0
  • Databases
  • Functions
  • Web
sharmnten
29 Dec, 2024, 17:30

Hi there,

I am new to Appwrite, and am trying to make a database storing a number associated with the userId. Here is my code.

Function call(Javascript) const payload = JSON.stringify({context: context, function:'get', userId: userId, amount: amount }); const result = await functions.createExecution("6770291b00171ec2611b", payload);

Function Code(Python): from appwrite.client import Client from appwrite.services.users import Users from appwrite.services.databases import Databases from appwrite.exception import AppwriteException import os key = os.API_KEY

This Appwrite function will be executed every time your function is triggered

def main(context,function, userId, amount): # You can use the Appwrite SDK to interact with other services # For this example, we're using the Users service client = ( Client() .set_endpoint('https://cloud.appwrite.io/v1') .set_project('67609b010021900fc6e6') .set_key(key) ) users = Users(client) databases = Databases(client) if(function == "get"): result = databases.get_document('6760b9c20030df251f1c','badgerBucks', userId) return res.result['badgerBucks'] elif(function == "set"): databases.update_document('6760b9c20030df251f1c','badgerBucks', userId, {'username': userId, 'badgerBucks': amount}) elif(function == "create"): database.creat_document('6760b9c20030df251f1c','badgerBucks',userId,{'username':userId,'badgerBucks':amount})

I am wondering if I should use the context parameter instead of the function parameter. I could not find much on the docs about how and what to use the context parameter for.

Thanks in advance,

-sharmnten

TL;DR
Context parameter in Appwrite functions holds Request, Response, Log, and Error vars. It's present in all runtimes. The user code provided in Python seems incorrect as it doesn't utilize the context object. The context parameter can be used to access runtime details like Request and Response data. Revising the Python code to utilize the context object correctly will enhance its functionality.
sharmnten
29 Dec, 2024, 17:31

EDIT: the markdown formatting of discord messed up the some of the comments. Apologies.

Darshan Pandya
29 Dec, 2024, 17:35

Functions need to have a specific default entrypoint that accepts just the context object or the de-structured objects within if the runtime supports it.

The context in all runtimes contain 4 vars. Request, Response, Log & Error.

See - https://appwrite.io/docs/products/functions/develop#context-object

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more