Chat-Endpunkt

Senden Sie Nachrichten an Ihren Agenten und erhalten Sie programmatisch KI-Antworten.

Der Chat-Endpunkt ermöglicht es Ihnen, Nachrichten an Ihren Agenten zu senden und KI-Antworten zu erhalten. Nutzen Sie ihn, um individuelle Chat-Oberflächen zu erstellen oder Agent-Funktionalität in Ihre Anwendungen zu integrieren.

Endpunkt

POST /api/v1/chat

Authentifizierung

Erfordert eine Authentifizierung per Bearer-Token.

Authorization: Bearer YOUR_API_KEY

Anfragetext

{
  "chatbotId": "uuid",
  "messages": [
    { "role": "user", "content": "What are your pricing plans?" }
  ],
  "conversationId": "optional-id",
  "stream": false
}

Parameter

FieldTypeRequiredDescription
chatbotIdstring (UUID)JaID Ihres Agenten
messagesarrayJaArray von Nachrichtenobjekten (1-100 Elemente)
conversationIdstring (max 16 chars)NeinReferenz-ID, die im x-conversation-id-Header eines vorherigen Aufrufs zurückgegeben wurde. Bei ungültigen oder fehlenden Werten weist der Server eine neue ID zu.
streambooleanNeinStreaming-Antwort aktivieren (Standard: false)

Nachrichtenobjekt

FieldTypeValuesDescription
rolestring"user", "assistant"Wer die Nachricht gesendet hat
contentstring1-32000 ZeichenNachrichtentext

Limits

  • Maximal 100 Nachrichten pro Anfrage
  • Maximal 32.000 Zeichen pro Nachricht

Antwort

Ohne Streaming (Standard)

Erfolg (200):

{
  "text": "Our pricing starts at $29.99/month for the Hobby plan..."
}

Enthält folgende Header:

X-Conversation-ID: abc123

Streaming

Setzen Sie "stream": true, um Streaming-Antworten zu aktivieren.

Erfolg (200):

Content-Type: text/plain; charset=utf-8

Die Antwort streamt reinen Text, während er generiert wird. Verwenden Sie dies für Echtzeit-UI-Updates.

Beispielanfragen

Einfache Anfrage

curl -X POST 'https://your-domain.com/api/v1/chat' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "chatbotId": "123e4567-e89b-12d3-a456-426614174000",
    "messages": [
      {"role": "user", "content": "What are your pricing plans?"}
    ]
  }'

Mehrteilige Unterhaltung

curl -X POST 'https://your-domain.com/api/v1/chat' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "chatbotId": "123e4567-e89b-12d3-a456-426614174000",
    "conversationId": "conv_abc123",
    "messages": [
      {"role": "user", "content": "What are your pricing plans?"},
      {"role": "assistant", "content": "We offer three plans..."},
      {"role": "user", "content": "Tell me more about the Pro plan"}
    ]
  }'

Streaming-Antwort

curl -X POST 'https://your-domain.com/api/v1/chat' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "chatbotId": "123e4567-e89b-12d3-a456-426614174000",
    "messages": [{"role": "user", "content": "Hello"}],
    "stream": true
  }'

Fehlerantworten

StatusMessageCause
400Invalid JSON bodyFehlerhaftes JSON
400Invalid request bodyFehlende oder ungültige Felder
401Missing or invalid Authorization headerHeader nicht angegeben oder falsches Format
401Invalid API keySchlüssel nicht erkannt
403API access requires a Hobby plan or above with active billingTarif oder Abrechnung erlaubt keinen API-Zugriff
403Chatbot does not belong to this accountAgent gehört zu einem anderen Konto
404Chatbot not foundUngültige Agent-ID
429Rate limit exceededZu viele Anfragen

Format der Fehlerantwort

{
  "message": "Invalid request body",
  "details": {
    "chatbotId": ["Required"]
  }
}

Codebeispiele

Node.js

async function sendMessage(chatbotId, message) {
  const response = await fetch('https://your-domain.com/api/v1/chat', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.AGENTKIT_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      chatbotId,
      messages: [{ role: 'user', content: message }],
    }),
  });

  if (!response.ok) {
    throw new Error(`API error: ${response.status}`);
  }

  const data = await response.json();
  return data.text;
}

Python

import requests

def send_message(chatbot_id: str, message: str) -> str:
    response = requests.post(
        'https://your-domain.com/api/v1/chat',
        headers={
            'Authorization': f'Bearer {API_KEY}',
            'Content-Type': 'application/json',
        },
        json={
            'chatbotId': chatbot_id,
            'messages': [{'role': 'user', 'content': message}],
        },
    )
    response.raise_for_status()
    return response.json()['text']

Node.js mit Streaming

async function streamMessage(chatbotId, message) {
  const response = await fetch('https://your-domain.com/api/v1/chat', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.AGENTKIT_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      chatbotId,
      messages: [{ role: 'user', content: message }],
      stream: true,
    }),
  });

  const reader = response.body.getReader();
  const decoder = new TextDecoder();

  while (true) {
    const { done, value } = await reader.read();
    if (done) break;

    const text = decoder.decode(value);
    process.stdout.write(text); // Print as it streams
  }
}

Unterhaltungen verwalten

Eine Unterhaltung starten

Lassen Sie conversationId weg, um eine neue Unterhaltung zu starten. Der Antwort-Header X-Conversation-ID enthält die neue ID.

Eine Unterhaltung fortsetzen

Geben Sie conversationId sowie alle vorherigen Nachrichten an, um den Kontext zu erhalten.

Nachrichtenverlauf

Sie müssen vorherige Nachrichten in jeder Anfrage mitsenden. Die API ist zustandslos – wir speichern zwischen den Anfragen keinen Unterhaltungsverlauf auf dem Server.

Nächste Schritte