startChat()

Creates a multi-turn chat session that automatically manages conversation history. Use sendMessage() to continue the conversation.

Syntax

gemini-api
const chat = model.startChat({ history?, generationConfig?, safetySettings? })

Example

gemini-api
const chat = model.startChat({
  history: [
    { role: "user", parts: [{ text: "I am building a Node.js REST API." }] },
    { role: "model", parts: [{ text: "Great! What would you like help with?" }] },
  ],
});

const response1 = await chat.sendMessage("What authentication should I use?");
console.log(response1.response.text());

// History is maintained automatically
const response2 = await chat.sendMessage("Show me a code example.");
console.log(response2.response.text());