The API reference describes the RESTful APIs you can use to interact with the Z.AI platform.
Z.AI provides standard HTTP API interfaces that support multiple programming languages and development environments, with SDKs also available.
API Endpoint
Z.ai Platform’s general API endpoint is as follows:
https://api.z.ai/api/paas/v4
Authentication
The Z.AI API uses the standard HTTP Bearer for authentication.
An API key is required, which you can create or manage on the API Keys Page.
API keys should be provided via HTTP Bearer Authentication in HTTP Request Headers.
Authorization: Bearer ZAI_API_KEY
Playground
The API Playground allows developers to quickly try out API calls. Simply click Try it on the API details page to get started.
- On the API details page, there are many interactive options, such as switching input types, switching tabs, and adding new content.
- You can click Add an item or Add new property to add more properties the API need.
- Note that when switching the tabs, the previous properties value you need re-input or re-switch.
Call Examples
cURL
Official Python SDK
Official Java SDK
OpenAI Python SDK
OpenAI NodeJs SDK
OpenAI Java SDK
curl -X POST "https://api.z.ai/api/paas/v4/chat/completions" \
-H "Content-Type: application/json" \
-H "Accept-Language: en-US,en" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "glm-4.7",
"messages": [
{
"role": "system",
"content": "You are a helpful AI assistant."
},
{
"role": "user",
"content": "Hello, please introduce yourself."
}
],
"temperature": 1.0,
"stream": true
}'
Install SDK# Install latest version
pip install zai-sdk
# Or specify version
pip install zai-sdk==0.1.0
Verify Installationimport zai
print(zai.__version__)
Usage Examplefrom zai import ZaiClient
# Initialize client
client = ZaiClient(api_key="YOUR_API_KEY")
# Create chat completion request
response = client.chat.completions.create(
model="glm-4.7",
messages=[
{
"role": "system",
"content": "You are a helpful AI assistant."
},
{
"role": "user",
"content": "Hello, please introduce yourself."
}
]
)
# Get response
print(response.choices[0].message.content)
Install SDKMaven<dependency>
<groupId>ai.z.openapi</groupId>
<artifactId>zai-sdk</artifactId>
<version>0.3.0</version>
</dependency>
Gradle (Groovy)implementation 'ai.z.openapi:zai-sdk:0.3.0'
Usage Exampleimport ai.z.openapi.ZaiClient;
import ai.z.openapi.service.model.*;
import java.util.Arrays;
public class QuickStart {
public static void main(String[] args) {
// Initialize client
ZaiClient client = ZaiClient.builder().ofZAI()
.apiKey("YOUR_API_KEY")
.build();
// Create chat completion request
ChatCompletionCreateParams request = ChatCompletionCreateParams.builder()
.model("glm-4.7")
.messages(Arrays.asList(
ChatMessage.builder()
.role(ChatMessageRole.USER.value())
.content("Hello, who are you?")
.build()
))
.stream(false)
.build();
// Send request
ChatCompletionResponse response = client.chat().createChatCompletion(request);
// Get response
System.out.println(response.getData().getChoices().get(0).getMessage().getContent());
}
}
Install SDK# Install or upgrade to latest version
pip install --upgrade 'openai>=1.0'
Verify Installationpython -c "import openai; print(openai.__version__)"
Usage Examplefrom openai import OpenAI
client = OpenAI(
api_key="your-Z.AI-api-key",
base_url="https://api.z.ai/api/paas/v4/"
)
completion = client.chat.completions.create(
model="glm-4.7",
messages=[
{"role": "system", "content": "You are a smart and creative novelist"},
{"role": "user", "content": "Please write a short fairy tale story as a fairy tale master"}
]
)
print(completion.choices[0].message.content)
Install SDK# Install or upgrade to latest version
npm install openai
# Or using yarn
yarn add openai
Usage Exampleimport OpenAI from "openai";
const client = new OpenAI({
apiKey: "your-Z.AI-api-key",
baseURL: "https://api.z.ai/api/paas/v4/"
});
async function main() {
const completion = await client.chat.completions.create({
model: "glm-4.7",
messages: [
{ role: "system", content: "You are a helpful AI assistant." },
{ role: "user", content: "Hello, please introduce yourself." }
]
});
console.log(completion.choices[0].message.content);
}
main();
Install SDKMaven<dependency>
<groupId>com.openai</groupId>
<artifactId>openai-java</artifactId>
<version>2.20.1</version>
</dependency>
Gradle (Groovy)implementation 'com.openai:openai-java:2.20.1'
Usage Exampleimport com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
public class QuickStart {
public static void main(String[] args) {
// Initialize client
OpenAIClient client = OpenAIOkHttpClient.builder()
.apiKey("your-Z.AI-api-key")
.baseUrl("https://api.z.ai/api/paas/v4/")
.build();
// Create chat completion request
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
.addSystemMessage("You are a helpful AI assistant.")
.addUserMessage("Hello, please introduce yourself.")
.model("glm-4.7")
.build();
// Send request and get response
ChatCompletion chatCompletion = client.chat().completions().create(params);
Object response = chatCompletion.choices().get(0).message().content();
System.out.println(response);
}
}