Customization

AI API Proxy Transformation

Complete guide to implementing request and response transformations for AI API Proxy. Learn data mapping, format conversion, field manipulation, and complex transformation pipelines.

🔃

Transformation Overview

API transformation allows you to modify requests and responses as they pass through the API Gateway. This enables compatibility between different API versions, data formats, and client requirements without modifying your backend services.

Data Transformation Pipeline

📥
Client Request
🔄
Request Transform
⚙️
Backend Process
🔄
Response Transform
📤
Client Response

Why Use Transformations?

  • API Evolution: Support multiple API versions simultaneously
  • Format Conversion: Transform between JSON, XML, and other formats
  • Schema Mapping: Map client schemas to backend schemas
  • Data Enrichment: Add computed fields or metadata
  • Legacy Integration: Connect modern clients with legacy backends
📝

Transformation Types

Here are the most common transformation types used in AI API Gateway implementations:

🔀 Field Mapping

Map fields from client format to backend format and vice versa.

{ "user_id": "id" } { "fullName": "name" }

🔢 Type Conversion

Convert data types like strings to numbers, dates to timestamps, etc.

"2026-03-15" → 1234567890 "123" → 123

Field Addition

Add computed fields, timestamps, or metadata to requests/responses.

added: { "timestamp": now() } enriched: { "user": data + profile }

🔍 Field Filtering

Remove sensitive fields or limit response to specific fields.

exclude: ["password", "token"] include: ["id", "name", "email"]

📦 Format Conversion

Convert between different data formats like JSON to XML.

JSON → XML JSON → CSV

🔗 Nested Restructuring

Flatten or nest data structures as needed.

{a:{b:c}} → {a.b:c} {user:{name:x}} → {name:x}
⚙️

Implementation Guide

Request Transformation

Transform incoming requests before they reach your backend:

const requestTransform = async (req, res, next) => {
    // Transform request body
    const originalBody = req.body;
    
    // Field mapping: client format → backend format
    req.body = {
        userId: originalBody.user_id || originalBody.id,
        fullName: originalBody.full_name || originalBody.name,
        message: originalBody.prompt || originalBody.message,
        timestamp: Date.now(),
        requestId: generateRequestId()
    };
    
    next();
};

// Apply to specific routes
app.post('/api/transform', requestTransform, proxyHandler);

Response Transformation

Transform backend responses before returning to clients:

const responseTransform = async (req, res, next) => {
    // Capture original send
    const originalSend = res.send;
    
    res.send = function(data) {
        let response = JSON.parse(data);
        
        // Transform response: backend format → client format
        response = {
            id: response.userId,
            name: response.fullName,
            result: response.output,
            model: response.model,
            usage: response.tokens
        };
        
        // Remove sensitive fields
        delete response.internalId;
        
        originalSend.call(this, JSON.stringify(response));
    };
    
    next();
};

Best Practices

  • Define transformations in configuration, not hardcoded
  • Test transformations thoroughly with various input types
  • Handle transformation errors gracefully
  • Document all transformation mappings
  • Consider performance impact of complex transformations
  • Use logging to debug transformation issues