Docs
User Management - JavaScript SDK
User Management - JavaScript SDK
Create and manage users with the JavaScript SDK
User Management
Create or Get User (Upsert)
import { LumnisClient } from 'lumnisai'
const client = new LumnisClient({ apiKey: process.env.LUMNISAI_API_KEY! })
// Create or update user
const user = await client.createUser({
email: "user@example.com",
firstName: "John",
lastName: "Doe"
})
console.log(`User ID: ${user.id}`)
console.log(`Email: ${user.email}`)Get User
// Get user by email
const user = await client.getUser("user@example.com")
// Get user by ID
const user = await client.getUser("550e8400-e29b-41d4-a716-446655440000")
console.log(`${user.email}: ${user.firstName} ${user.lastName}`)List Users
// List users with pagination
const users = await client.listUsers({
page: 1,
pageSize: 50
})
console.log(`Total users: ${users.total}`)
for (const user of users.users) {
console.log(`${user.email}: ${user.firstName} ${user.lastName}`)
if (user.metadata) {
console.log(` Metadata: ${JSON.stringify(user.metadata)}`)
}
}Update User
// Update user details
const updatedUser = await client.updateUser(
"user@example.com",
{
firstName: "Jane",
lastName: "Smith"
}
)
console.log(`Updated: ${updatedUser.firstName} ${updatedUser.lastName}`)Delete User
// Soft delete user
await client.deleteUser("user@example.com")User-Scoped Operations
For User Method
import { LumnisClient } from 'lumnisai'
const client = new LumnisClient({ apiKey: process.env.LUMNISAI_API_KEY! })
// Create a user-scoped client
const userClient = client.forUser("user@example.com")
// All operations now scoped to this user
const response = await userClient.invoke("What's the weather?")
// List user's threads
const threads = await userClient.listThreads()
// Upload file for user
const file = await userClient.files.upload(fileStream, {
scope: 'user'
})Multi-User Workflows
Multi-User Conversation
import { LumnisClient } from 'lumnisai'
const client = new LumnisClient({ apiKey: process.env.LUMNISAI_API_KEY! })
// Create users
const alice = await client.createUser({
email: "alice@example.com",
firstName: "Alice"
})
const bob = await client.createUser({
email: "bob@example.com",
firstName: "Bob"
})
// Alice starts a conversation
const response1 = await client.responses.create({
messages: [{ role: 'user', content: "What's the weather like?" }],
userId: alice.email
})
const threadId = response1.threadId
// Bob continues the conversation
const response2 = await client.responses.create({
threadId,
messages: [{ role: 'user', content: "What about tomorrow?" }],
userId: bob.email
})
// Get full conversation
const thread = await client.getThread(threadId)
const allResponses = await client.threads.getResponses(threadId)
console.log(`Thread: ${thread.title}`)
for (const resp of allResponses) {
const user = await client.getUser(resp.userId)
console.log(`${user.firstName}: ${resp.outputText}`)
}Complete User Management Example
import { LumnisClient, type AgentConfig } from 'lumnisai'
// Initialize client
const client = new LumnisClient({ apiKey: process.env.LUMNISAI_API_KEY! })
// Create multiple users
const userData = [
{ email: "alice@example.com", firstName: "Alice", lastName: "Johnson" },
{ email: "bob@example.com", firstName: "Bob", lastName: "Smith" },
{ email: "charlie@example.com", firstName: "Charlie", lastName: "Brown" }
]
for (const data of userData) {
const user = await client.createUser(data)
console.log(`Created: ${user.email}`)
}
// List all users
const allUsers = await client.listUsers({ pageSize: 100 })
console.log(`\nTotal users: ${allUsers.total}`)
// Perform operations for specific user
const aliceClient = client.forUser("alice@example.com")
// Create response for Alice
const response = await aliceClient.invoke(
"What are the latest AI trends?",
{
agentConfig: {
coordinatorModelName: "openai:gpt-4.1",
useCognitiveTools: true
}
}
)
console.log(`\nAlice's response: ${response.outputText.substring(0, 100)}...`)
// List Alice's threads
const threads = await aliceClient.listThreads()
console.log(`Alice has ${threads.threads.length} threads`)
// Update user information
const updated = await client.updateUser(
"bob@example.com",
{ firstName: "Robert" }
)
console.log(`\nUpdated user: ${updated.firstName} ${updated.lastName}`)
// Get user by ID
const user = await client.getUser(updated.id)
console.log(`Retrieved by ID: ${user.email}`)Best Practices
Error Handling
import { LumnisClient, NotFoundError, ValidationError } from 'lumnisai'
const client = new LumnisClient({ apiKey: process.env.LUMNISAI_API_KEY! })
try {
const user = await client.getUser("nonexistent@example.com")
}
catch (error) {
if (error instanceof NotFoundError) {
// Create user if not found
const user = await client.createUser({
email: "nonexistent@example.com",
firstName: "New",
lastName: "User"
})
console.log(`Created user: ${user.email}`)
}
else if (error instanceof ValidationError) {
console.error(`Validation error: ${error.details}`)
}
}Pagination
// Get all users with pagination
const allUsers: any[] = []
let page = 1
const pageSize = 50
while (true) {
const result = await client.listUsers({ page, pageSize })
allUsers.push(...result.users)
if (result.users.length < pageSize) {
break
}
page++
}
console.log(`Total users retrieved: ${allUsers.length}`)Use User Scoping
Always specify userId or use user-scoped clients for production applications:
// Good: User-scoped operations
const response = await client.invoke(
"Hello!",
{ userId: "user@example.com" }
)
// Or use scoped client
const userClient = client.forUser("user@example.com")
const response = await userClient.invoke("Hello!")