Skip to content

Examples

Standard cURL request

bash
curl -X POST "https://your-kitemesh.example.com/v1/chat/completions" \
  -H "Authorization: Bearer km_your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "group:123e4567-e89b-12d3-a456-426614174000",
    "messages": [
      {
        "role": "user",
        "content": "Give a summary of the key points."
      }
    ],
    "metadata": {
      "request_id": "req_demo_001",
      "mode": "auto"
    }
  }'

Streaming cURL request

bash
curl -N -X POST "https://your-kitemesh.example.com/v1/chat/completions" \
  -H "Authorization: Bearer km_your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "group:123e4567-e89b-12d3-a456-426614174000",
    "stream": true,
    "messages": [
      {
        "role": "user",
        "content": "Prepare a detailed response."
      }
    ]
  }'

Resume after approval

bash
curl -X POST "https://your-kitemesh.example.com/v1/chat/resume" \
  -H "Authorization: Bearer km_your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "group:123e4567-e89b-12d3-a456-426614174000",
    "approval_id": "86dd028d-6f5c-4f13-83db-d4fe9bc5b865",
    "approved_tool_execution_ids": [
      "1fb2fd25-8f78-412a-a0b8-7976e3700fc4"
    ],
    "messages": [
      {
        "role": "user",
        "content": "You can continue."
      }
    ]
  }'

TypeScript example

ts
type ChatCompletionResponse = {
  choices: Array<{
    message?: {
      role: string;
      content: string | null;
      tool_calls?: Array<{
        id: string;
        type: string;
        function: {
          name: string;
          arguments: string;
        };
      }>;
    };
    finish_reason: string | null;
  }>;
  kitemesh?: {
    approval_id?: string;
    execution_id?: string;
    expires_at?: string;
    request_id?: string;
  };
};

async function runChat() {
  const response = await fetch(
    "https://your-kitemesh.example.com/v1/chat/completions",
    {
      method: "POST",
      headers: {
        Authorization: "Bearer km_your_token_here",
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        model: "group:123e4567-e89b-12d3-a456-426614174000",
        messages: [
          {
            role: "user",
            content: "Create a three-step action plan.",
          },
        ],
        metadata: {
          request_id: "req_demo_002",
          mode: "auto",
        },
      }),
    },
  );

  const payload = (await response.json()) as ChatCompletionResponse;

  if (!response.ok) {
    throw new Error("Chat request failed");
  }

  const choice = payload.choices[0];

  if (choice.finish_reason === "tool_calls") {
    console.log("Approval required", payload.kitemesh?.approval_id);
    return;
  }

  console.log(choice.message?.content ?? "");
}