Oystehr
XML Responses

Zambda XML Response Support

Zambda Functions can return XML responses by setting the appropriate content-type header.

Overview

By default, Zambda responses are returned in JSON format with a standard structure:

{
  "status": 200,
  "output": {
    "message": "Hello from JSON"
  }
}

When you need to return XML instead, simply set the Content-Type header to application/xml or text/xml, and the response will be automatically formatted as XML.

How It Works

  1. Zambda returns XML content-type header - Set Content-Type to application/xml or text/xml
  2. Response is wrapped in XML - The status and output are wrapped in an XML structure
  3. SDK parses automatically - The Oystehr SDK automatically detects and parses XML responses

Examples

Example 1: Returning Raw XML String

exports.index = async (event) => {
  return {
    statusCode: 200,
    headers: {
      'Content-Type': 'application/xml'
    },
    body: '<message>Hello from XML!</message><timestamp>2025-12-23T10:30:00Z</timestamp>'
  };
};

Response sent to client:

<response>
  <status>200</status>
  <output><message>Hello from XML!</message><timestamp>2025-12-23T10:30:00Z</timestamp></output>
</response>

Example 2: Returning Object (Auto-serialized to XML)

exports.index = async (event) => {
  return {
    statusCode: 200,
    headers: {
      'Content-Type': 'application/xml'
    },
    body: JSON.stringify({
      patient: {
        id: '12345',
        name: 'John Doe',
        status: 'checked-in'
      }
    })
  };
};

Response sent to client:

<response>
  <status>200</status>
  <output>
    <patient>
      <id>12345</id>
      <name>John Doe</name>
      <status>checked-in</status>
    </patient>
  </output>
</response>

Example 3: Dynamic Format Based on Request

exports.index = async (event) => {
  const body = event.body ? JSON.parse(event.body) : {};
  const format = body.format || 'json';
 
  const responseData = {
    message: 'Hello!',
    timestamp: new Date().toISOString()
  };
 
  if (format === 'xml') {
    return {
      statusCode: 200,
      headers: {
        'Content-Type': 'application/xml'
      },
      body: `<message>${responseData.message}</message><timestamp>${responseData.timestamp}</timestamp>`
    };
  }
 
  // Default to JSON
  return {
    statusCode: 200,
    body: JSON.stringify(responseData)
  };
};

Using with the Oystehr SDK

The SDK automatically handles both JSON and XML responses:

import Oystehr from '@oystehr/sdk';
 
const client = new Oystehr({
  accessToken: 'your-token',
  projectId: 'your-project-id'
});
 
// Execute zambda that returns XML
const result = await client.zambda.execute({
  id: 'your-xml-returning-zambda-id',
});
 
// SDK automatically parses XML responses
console.log(result);
Status: 200
Output type: string
Output: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response><message>Hello from XML</message><timestamp>2025-12-23T22:54:42.538Z</timestamp></response>"