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.
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()
Function Signature
langbase.threads.create(options);
// with types
langbase.threads.create(options: ThreadsCreate);
- Name
options
- Type
- ThreadsCreate
- Description
ThreadsCreate Object
threadId?: string; metadata?: Record<string, string>; messages?: ThreadMessage[];
Following are the properties of the options object.
- Name
threadId
- Type
- string
- Description
An optional thread ID. If not provided, a new one will be generated.
- Name
metadata
- Type
- Record<string, string>
- Description
A record of metadata to associate with the thread.
- Name
messages
- Type
- ThreadMessage[]
- Description
A list of initial messages for the thread.
ThreadMessage
is an object that extends theMessage
object.ThreadMessage
attachments?: any[]; metadata?: Record<string, string>; role: 'user' | 'assistant' | 'system' | 'tool'; content: string | null; name?: string; tool_call_id?: string; tool_calls?: ToolCall[];
Usage example
Install the SDK
npm i langbase
Environment variables
Environment variables
LANGBASE_API_KEY="<USER/ORG-API-KEY>"
Create a thread
Create a thread on Langbase
const thread = await langbase.threads.create({
metadata: {
userId: "user123",
topic: "support"
},
messages: [{
role: "user",
content: "Hello, I need help!"
}]
});
- Name
ThreadsBaseResponse
- Type
- object
- Description
The response of the
threads.create()
function is aPromise
that resolves to aThreadsBaseResponse
object.ThreadsBaseResponse
id: string; object: 'thread'; created_at: number; metadata: Record<string, string>;
ThreadsBaseResponse type of langbase.threads.create()
{
"id": "thread_123",
"object": "thread",
"created_at": 1709544000,
"metadata": {
"userId": "user123",
"topic": "support"
}
}