Learn how to create and manage templates that format AI-generated content into structured formats like JSON or custom text layouts.

What are Templates?

Templates define the output structure for generated content. They support:

  • JSON output: For structured data that can be consumed by applications
  • Text output: For human-readable formatted content
  • Variables in {{variable}} format that get replaced with generated content

Creating Templates

const API_URL = 'https://sdk.senso.ai/api/v1';
const API_KEY = 'YOUR_API_KEY';

async function createTemplates() {
  try {
    // Create a JSON template for product data
    const jsonResponse = await fetch(`${API_URL}/templates`, {
      method: 'POST',
      headers: {
        'X-API-Key': API_KEY,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        name: 'Product JSON Template',
        text: `{
  "product": {
    "name": "{{product_name}}",
    "summary": "{{summary}}",
    "features": {{features}},
    "benefits": {{benefits}},
    "price_range": "{{price_range}}",
    "target_audience": "{{target_audience}}"
  }
}`,
        output_type: 'json'
      })
    });
    
    const jsonTemplate = await jsonResponse.json();
    console.log('JSON template created:', jsonTemplate.template_id);

    // Create a text template for email content
    const emailResponse = await fetch(`${API_URL}/templates`, {
      method: 'POST',
      headers: {
        'X-API-Key': API_KEY,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        name: 'Customer Email Template',
        text: `Subject: {{subject}}

Dear {{customer_name}},

{{greeting}}

{{main_content}}

Key Points:
{{key_points}}

{{call_to_action}}

Best regards,
{{sender_name}}
{{company_name}}`,
        output_type: 'text'
      })
    });
    
    const emailTemplate = await emailResponse.json();
    console.log('Email template created:', emailTemplate.template_id);
    
  } catch (error) {
    console.error('Error creating templates:', error);
  }
}

createTemplates();

More Template Examples

FAQ Template (JSON)

{
  "name": "FAQ JSON Template",
  "text": "{\"faqs\": [{{faq_items}}], \"category\": \"{{category}}\", \"last_updated\": \"{{date}}\"}",
  "output_type": "json"
}

Report Template (Text)

{
  "name": "Analysis Report Template",
  "text": "ANALYSIS REPORT\n================\n\nDate: {{date}}\nAnalyst: {{analyst}}\n\nExecutive Summary:\n{{executive_summary}}\n\nDetailed Findings:\n{{findings}}\n\nRecommendations:\n{{recommendations}}\n\nConclusion:\n{{conclusion}}",
  "output_type": "text"
}

Managing Templates

// List all templates
const templatesResponse = await fetch(`${API_URL}/templates?limit=10`, {
  headers: { 'X-API-Key': API_KEY }
});
const templates = await templatesResponse.json();
console.log('Available templates:', templates);

// Get a specific template
const templateResponse = await fetch(`${API_URL}/templates/${templateId}`, {
  headers: { 'X-API-Key': API_KEY }
});
const template = await templateResponse.json();
console.log('Template details:', template);

// Update a template
const updateResponse = await fetch(`${API_URL}/templates/${templateId}`, {
  method: 'PUT',
  headers: {
    'X-API-Key': API_KEY,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: 'Enhanced Product JSON Template',
    text: '{"product": {"name": "{{name}}", "details": {{details}}}}',
    output_type: 'json'
  })
});
const updatedTemplate = await updateResponse.json();

// Delete a template
await fetch(`${API_URL}/templates/${templateId}`, {
  method: 'DELETE',
  headers: { 'X-API-Key': API_KEY }
});

Best Practices

  1. Use descriptive variable names: Make it clear what content should replace each variable
  2. Validate JSON templates: Ensure your JSON structure is valid before creating the template
  3. Keep templates focused: Create separate templates for different use cases rather than one complex template
  4. Version your templates: Consider including version numbers in template names for tracking changes

Next Steps