Morpheus API Gateway LogoMorpheus API Gateway
Chat

Create Chat Completion

Create a chat completion with automatic session creation if enabled.

POST
/api/v1/chat/completions

Authorization

Authorization<token>

🗝️ API key in format: 'Bearer sk-xxxxxx'

In: header

Request Body

application/jsonRequired
modelstring | null | null
messagesRequiredMessages
temperaturenumber | null | null
top_pnumber | null | null
ninteger | null | null
streamboolean | null | null
stopstring | array<unknown> | null | null
max_tokensinteger | null | null
presence_penaltynumber | null | null
frequency_penaltynumber | null | null
toolsarray<unknown> | null | null
tool_choicestring | object | null | null
session_idstring | null | null
[key: string]any

Response Body

Successful Response

TypeScript Definitions

Use the response body type in TypeScript.

responseRequiredunknown

Refused before forwarding: error.code is model_routing_error. X-Morpheus-Error repeats the code.

TypeScript Definitions

Use the response body type in TypeScript.

errorRequiredChatErrorDetail

OpenAI-style error body; the code is repeated in the X-Morpheus-Error response header.

Validation Error

TypeScript Definitions

Use the response body type in TypeScript.

detailDetail
curl -X POST "https://example.com/api/v1/chat/completions" \
  -H "Authorization: <token>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "llama-3.3-70b",
    "messages": [
      {
        "role": "user",
        "content": "Hello"
      }
    ]
  }'
const body = JSON.stringify({
  "model": "llama-3.3-70b",
  "messages": [
    {
      "role": "user",
      "content": "Hello"
    }
  ]
})

fetch("https://example.com/api/v1/chat/completions", {
  headers: {
    "Authorization": "<token>"
  },
  body
})
package main

import (
  "fmt"
  "net/http"
  "io/ioutil"
  "strings"
)

func main() {
  url := "https://example.com/api/v1/chat/completions"
  body := strings.NewReader(`{
    "model": "llama-3.3-70b",
    "messages": [
      {
        "role": "user",
        "content": "Hello"
      }
    ]
  }`)
  req, _ := http.NewRequest("POST", url, body)
  req.Header.Add("Authorization", "<token>")
  req.Header.Add("Content-Type", "application/json")
  res, _ := http.DefaultClient.Do(req)
  defer res.Body.Close()
  body, _ := ioutil.ReadAll(res.Body)

  fmt.Println(res)
  fmt.Println(string(body))
}
import requests

url = "https://example.com/api/v1/chat/completions"
body = {
  "model": "llama-3.3-70b",
  "messages": [
    {
      "role": "user",
      "content": "Hello"
    }
  ]
}
response = requests.request("POST", url, json = body, headers = {
  "Authorization": "<token>",
  "Content-Type": "application/json"
})

print(response.text)
null
{
  "error": {
    "message": "string",
    "type": "string",
    "code": "string",
    "param": "string"
  }
}
{
  "detail": [
    {
      "loc": [
        "string"
      ],
      "msg": "string",
      "type": "string"
    }
  ]
}

How is this guide?