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
The request payload containing the user’s message and optional configuration Show AnswerRequest properties
The user’s message or question to process. Cannot be empty or whitespace-only.
model
'gpt-4' | 'gpt-4-turbo' | 'gpt-3.5-turbo'
The OpenAI model to use for this request. Overrides the client’s defaultModel if specified.
Maximum number of tokens to generate. Note: This may be overridden by brand-specific settings.
Sampling temperature between 0 and 2. Higher values make output more random. Note: This may be overridden by brand-specific settings.
Additional instructions to guide the AI’s response style or content.
Unique ID to track conversation context across multiple requests.
Array of previous messages in the conversation for context. role
'user' | 'assistant'
required
The role of the message sender.
The content of the message.
Optional DOM targets for automatic UI updates and impression tracking Target element for the response text. Can be an element ID string or HTMLElement reference.
Target element for the brand link. Can be an element ID string or HTMLElement reference.
Response
The generated AI response text with brand enrichment applied.
Metadata about the response and brand enrichment Show AnswerMetadata properties
Information about the brand used for enrichment Show BrandInfo properties
Unique identifier for the brand
Domain of the brand’s website
Affiliate or tracking link for the brand
Tracking code for impression registration
Similarity score (0-1) indicating how well the message matched the brand’s triggers
List of trigger phrases configured for the matched brand
List of trigger phrases that matched in the user’s message
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
The method validates the message is not empty
Sends a POST request to /v1/answer with the request payload
Waits for the complete response from the API
If targets are provided, automatically updates the DOM and registers an impression
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