Create Thread langbase.threads.create()
You can use the threads.create()
function to create new conversation threads. Threads help you organize and maintain conversation history, making it easier to build conversational applications.
Generate a User/Org API key
You will need to generate an API key to authenticate your requests. For more information, visit the User/Org API key documentation.
API reference
langbase.threads.create(options)
Create a new thread with optional initial messages and metadata.
Function Signature
langbase.threads.create(options);
// with types
langbase.threads.create(options: ThreadsCreate);
options
- Name
options
- Type
- ThreadsCreate
- Description
ThreadsCreate Object
interface ThreadsCreate { threadId?: string; metadata?: Record<string, string>; messages?: ThreadMessage[]; } interface ThreadMessage extends Message { attachments?: any[]; metadata?: Record<string, string>; } interface Message { role: 'user' | 'assistant' | 'system' | 'tool'; content: string | null; name?: string; tool_call_id?: string; tool_calls?: ToolCall[]; } interface ToolCall { id: string; type: 'function'; function: Function; } interface Function { name: string; arguments: string; }
Usage example
Install the SDK
npm i langbase
Environment variables
.env file
LANGBASE_API_KEY="<USER/ORG-API-KEY>"
langbase.threads.create()
example
Create Example
const thread = await langbase.threads.create({
metadata: {
userId: "user123",
topic: "support"
},
messages: [{
role: "user",
content: "Hello, I need help!"
}]
});
Response
The response of the threads.create()
function is a Promise
that resolves to a ThreadsBaseResponse
object.
ThreadsBaseResponse
interface ThreadsBaseResponse {
id: string;
object: 'thread';
created_at: number;
metadata: Record<string, string>;
}
Response Example
{
"id": "thread_123",
"object": "thread",
"created_at": 1709544000,
"metadata": {
"userId": "user123",
"topic": "support"
}
}