ZtoApi Documentation

Contents

Overview

This is an OpenAI-compatible API proxy for Z.ai GLM-4.5, GLM-4.6, and GLM-4.5V models. It supports OpenAI API format with streaming and non-streaming responses.

OpenAI Base URL: http://localhost:9090/v1

Note: Default port is 9090 and can be changed via PORT environment variable.

Authentication

All API requests must include a valid API key in the request header:

Authentication: Authorization: Bearer your-api-key

The default API key is sk-your-key. Change it with the DEFAULT_KEY environment variable.

OpenAI API Endpoints

Get Models

GET /v1/models

Returns the list of available models.

Request Parameters

None

{ "object": "list", "data": [ { "id": "GLM-4.5", "object": "model", "created": 1756788845, "owned_by": "z.ai" } ] }

Chat Completions

POST /v1/chat/completions

Generate model responses from a list of messages. Supports streaming and non-streaming modes.

Request Parameters

Parameter Type Required Description
model string yes Model ID to use, e.g. "GLM-4.5"
messages array yes List of messages containing role and content
stream boolean no Whether to use streaming responses; default true
temperature number no Sampling temperature to control randomness
max_tokens integer no Maximum tokens to generate

Message Format

Field Type Description
role string Message role: system, user, assistant
content string Message content

Examples

Python Example

import openai # Configure client client = openai.OpenAI( api_key="your-api-key", # corresponds to DEFAULT_KEY base_url="http://localhost:9090/v1" ) # Non-streaming example - GLM-4.5 response = client.chat.completions.create( model="GLM-4.5", messages=[{"role": "user", "content": "Hello, please introduce yourself"}] ) print(response.choices[0].message.content)

cURL Example

# Non-streaming curl -X POST http://localhost:9090/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer your-api-key" \ -d '{ "model": "GLM-4.5", "messages": [{"role": "user", "content": "Hello"}], "stream": false }' # Streaming curl -X POST http://localhost:9090/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer your-api-key" \ -d '{ "model": "GLM-4.5", "messages": [{"role": "user", "content": "Hello"}], "stream": true }'

JavaScript Example

const fetch = require('node-fetch'); async function chatWithGLM(message, stream = false) { const response = await fetch('http://localhost:9090/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer your-api-key' }, body: JSON.stringify({ model: 'GLM-4.5', messages: [{ role: 'user', content: message }], stream: stream }) }); if (stream) { const reader = response.body.getReader(); const decoder = new TextDecoder(); while (true) { const { done, value } = await reader.read(); if (done) break; const chunk = decoder.decode(value); const lines = chunk.split('\n'); for (const line of lines) { if (line.startsWith('data: ')) { const data = line.slice(6); if (data === '[DONE]') { console.log('\nStream complete'); return; } try { const parsed = JSON.parse(data); const content = parsed.choices[0]?.delta?.content; if (content) { process.stdout.write(content); } } catch (e) { // ignore parse errors } } } } } else { const data = await response.json(); console.log(data.choices[0].message.content); } } // Example usage chatWithGLM('Hello, please introduce JavaScript', false);

Error Handling

The API uses standard HTTP status codes to denote success or failure:

Status Description
200 OK Request succeeded
400 Bad Request Request malformed or invalid parameters
401 Unauthorized API key invalid or missing
502 Bad Gateway Upstream service error
Note: In debug mode the server logs detailed information. Enable with DEBUG_MODE=true.