Z.AI provides standard HTTP API interfaces that support multiple programming languages and development environments, allowing you to easily integrate Z.AI’s powerful capabilities.

Core Advantages

Cross-platform Compatible

Supports all programming languages and platforms that support HTTP protocol

Standard Protocol

Based on RESTful design, follows HTTP standards, easy to understand and use

Flexible Integration

Can be integrated into any existing applications and systems

Real-time Calls

Supports synchronous and asynchronous calls to meet different scenario requirements

Get API Key

  1. Access Z.AI Open Platform, Register or Login.
  2. Create an API Key in the API Keys management page.
  3. Copy your API Key for use.

API Basic Information

Request URL

https://api.z.ai/api/paas/v4/

Request Header Requirements

Content-Type: application/json
Authorization: Bearer YOUR_API_KEY

Supported Authentication Methods

The simplest authentication method, directly using your API Key:
curl --location 'https://api.z.ai/api/paas/v4/chat/completions' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Accept-Language: en-US,en' \
--header 'Content-Type: application/json' \
--data '{
    "model": "glm-4.5",
    "messages": [
        {
            "role": "user",
            "content": "Hello"
        }
    ]
}'

Basic Call Examples

Simple Conversation

curl --location 'https://api.z.ai/api/paas/v4/chat/completions' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Accept-Language: en-US,en' \
--header 'Content-Type: application/json' \
--data '{
    "model": "glm-4.5",
    "messages": [
        {
            "role": "user",
            "content": "Please introduce the development history of artificial intelligence"
        }
    ],
    "temperature": 0.6,
    "max_tokens": 1024
}'

Streaming Response

curl --location 'https://api.z.ai/api/paas/v4/chat/completions' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Accept-Language: en-US,en' \
--header 'Content-Type: application/json' \
--data '{
    "model": "glm-4.5",
    "messages": [
        {
            "role": "user",
            "content": "Write a poem about spring"
        }
    ],
    "stream": true
}'

Multi-turn Conversation

curl --location 'https://api.z.ai/api/paas/v4/chat/completions' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Accept-Language: en-US,en' \
--header 'Content-Type: application/json' \
--data '{
    "model": "glm-4.5",
    "messages": [
        {
            "role": "system",
            "content": "You are a professional programming assistant"
        },
        {
            "role": "user",
            "content": "What is recursion?"
        },
        {
            "role": "assistant",
            "content": "Recursion is a programming technique where a function calls itself to solve problems..."
        },
        {
            "role": "user",
            "content": "Can you give me an example of Python recursion?"
        }
    ]
}'

Common Programming Language Examples

import requests
import json

def call_zai_api(messages, model="glm-4.5"):
    url = "https://api.z.ai/api/paas/v4/chat/completions"

    headers = {
        "Authorization": "Bearer YOUR_API_KEY",
        "Content-Type": "application/json",
        "Accept-Language": "en-US,en"
    }

    data = {
        "model": model,
        "messages": messages,
        "temperature": 0.6
    }

    response = requests.post(url, headers=headers, json=data)

    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"API call failed: {response.status_code}, {response.text}")

# Usage example
messages = [
    {"role": "user", "content": "Hello, please introduce yourself"}
]

result = call_zai_api(messages)
print(result['choices'][0]['message']['content'])

Best Practices

Security

  • Properly secure API Keys, do not hard-code them in your code
  • Use environment variables or configuration files to store sensitive information
  • Regularly rotate API Keys

Performance Optimization

  • Implement connection pooling and session reuse
  • Set reasonable timeout values
  • Use asynchronous requests for high-concurrency scenarios

Error Handling

  • Implement exponential backoff retry mechanisms
  • Log detailed error information
  • Set reasonable timeout and retry limits

Monitoring

  • Monitor API call frequency and success rates
  • Track response times and error rates
  • Set up alerting mechanisms

Get More

It is recommended to use HTTPS protocol in production environments and implement appropriate security measures to protect your API keys and data transmission.