Skip to main content
POST
/
action
/
serverless_function
Call Serverless Function
curl --request POST \
  --url https://api-lr.agent.ai/v1/action/serverless_function \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data @- <<EOF
{
  "language": "python",
  "serverless_code": "import json\n\ndef lambda_handler(event, context):\n  agent_context = {}\n  \n  # Handle POST requests\n  if event and 'body' in event and event['body']:\n      try:\n          event_json = json.loads(event['body'])\n          agent_context = event_json.get('context', event_json)\n      except json.JSONDecodeError:\n          # Handle case where body isn't valid JSON\n          agent_context = {\"error\": \"Invalid JSON in request body\"}\n  \n  # Handle GET requests\n  elif event and 'queryStringParameters' in event and event['queryStringParameters']:\n      agent_context = event['queryStringParameters']\n  \n  # Handle API Gateway format for both\n  elif event and 'pathParameters' in event and event['pathParameters']:\n      agent_context = event['pathParameters']\n  \n  # Prepare response\n  body = {\n      \"message\": \"Go Agent.AI Serverless Python Functions v1.0! Your function executed successfully!\",\n      \"input\": event,\n      \"context\": agent_context\n  }\n  \n  # Include CORS headers for browser access\n  headers = {\n      \"Content-Type\": \"application/json\",\n      \"Access-Control-Allow-Origin\": \"*\",\n      \"Access-Control-Allow-Headers\": \"Content-Type\",\n      \"Access-Control-Allow-Methods\": \"OPTIONS,POST,GET\"\n  }\n  \n  response = {\n      \"statusCode\": 200,\n      \"headers\": headers,\n      \"body\": json.dumps(body)\n  }\n  \n  return response\n",
  "output_variable_name": "<string>"
}
EOF
{
  "status": 123,
  "response": {}
}

Authorizations

Authorization
string
header
required

Bearer token from your account (https://agent.ai/user/integrations#api)

Body

application/json
language
enum<string>
default:python
required

Select the language for the serverless function, such as 'Python' or 'Node'.

Available options:
python,
node
serverless_code
string
default:import json def lambda_handler(event, context): agent_context = {} # Handle POST requests if event and 'body' in event and event['body']: try: event_json = json.loads(event['body']) agent_context = event_json.get('context', event_json) except json.JSONDecodeError: # Handle case where body isn't valid JSON agent_context = {"error": "Invalid JSON in request body"} # Handle GET requests elif event and 'queryStringParameters' in event and event['queryStringParameters']: agent_context = event['queryStringParameters'] # Handle API Gateway format for both elif event and 'pathParameters' in event and event['pathParameters']: agent_context = event['pathParameters'] # Prepare response body = { "message": "Go Agent.AI Serverless Python Functions v1.0! Your function executed successfully!", "input": event, "context": agent_context } # Include CORS headers for browser access headers = { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "Content-Type", "Access-Control-Allow-Methods": "OPTIONS,POST,GET" } response = { "statusCode": 200, "headers": headers, "body": json.dumps(body) } return response
required

Enter the serverless function code here, using Python or Node syntax to perform specific tasks.

output_variable_name
string
required

Provide a variable name to store the result of the serverless function, like 'function_result' or 'api_response'.

Pattern: ^[a-zA-Z][a-zA-Z0-9_]*$

Response

Successful response

status
integer<int32>

HTTP status code of the action response

response
object

Response data from the action