Skip to main content

Method Signature

async answer(
  request: AnswerRequest,
  targets?: Targets
): Promise<AnswerResponse>
Generate a complete AI response with brand enrichment in a single request. Use this method when you want to receive the full response at once rather than streaming it.

Parameters

request
AnswerRequest
required
The request payload containing the user’s message and optional configuration
targets
Targets
Optional DOM targets for automatic UI updates and impression tracking

Response

response
string
required
The generated AI response text with brand enrichment applied.
metadata
AnswerMetadata
required
Metadata about the response and brand enrichment

Examples

Basic Usage

const response = await client.answer({
  message: 'What are the best running shoes for marathon training?'
});

console.log(response.response);
console.log('Brand used:', response.metadata.brandUsed?.name);

With Custom Model

const response = await client.answer({
  message: 'Explain quantum computing',
  model: 'gpt-4-turbo',
  temperature: 0.7,
  maxTokens: 500
});

With Conversation Context

const response = await client.answer({
  message: 'What about trail running?',
  conversationId: 'conv_123',
  previousMessages: [
    {
      role: 'user',
      content: 'What are the best running shoes?'
    },
    {
      role: 'assistant',
      content: 'For road running, I recommend...'
    }
  ]
});

With DOM Targets

// Automatically update DOM elements with the response
const response = await client.answer(
  {
    message: 'Best laptops for programming?'
  },
  {
    text: 'response-container',
    link: 'brand-link'
  }
);

// Or using HTMLElement references
const textElement = document.getElementById('response-container');
const linkElement = document.getElementById('brand-link');

const response = await client.answer(
  {
    message: 'Best laptops for programming?'
  },
  {
    text: textElement,
    link: linkElement
  }
);

With Custom Instructions

const response = await client.answer({
  message: 'Tell me about electric cars',
  instructions: 'Focus on environmental benefits and cost savings. Keep response under 200 words.'
});

Error Handling

The method throws errors in the following cases:
try {
  const response = await client.answer({
    message: '  ' // Empty or whitespace-only message
  });
} catch (error) {
  console.error('Error:', error.message);
  // Output: "Message is required"
}

Network and Timeout Errors

import { NetworkError, TimeoutError } from '@thred/sdk';

try {
  const response = await client.answer({
    message: 'What are the best headphones?'
  });
} catch (error) {
  if (error instanceof TimeoutError) {
    console.error('Request timed out');
  } else if (error instanceof NetworkError) {
    console.error('Network error:', error.message);
  } else {
    console.error('API error:', error);
  }
}

How It Works

  1. The method validates the message is not empty
  2. Sends a POST request to /v1/answer with the request payload
  3. Waits for the complete response from the API
  4. If targets are provided, automatically updates the DOM and registers an impression
  5. Returns the full AnswerResponse with text and metadata

When to Use

  • Use answer() when you need the complete response at once
  • Use answerStream() for real-time streaming with a callback function
  • Use answerStreamGenerator() for streaming with async/await syntax

Build docs developers (and LLMs) love