> For the complete documentation index, see [llms.txt](https://docs.tensorx.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tensorx.ai/api-reference/api-examples.md).

# API Examples

Sample curl commands to get you started with the TensorX API. All examples use the OpenAI-compatible endpoint.

{% hint style="info" %}
Replace `YOUR_API_KEY` with your actual TensorX API key from [app.tensorx.ai](https://app.tensorx.ai).
{% endhint %}

***

## Basic Chat Completion

A simple chat request with GLM-5.1:

```bash
curl https://api.tensorx.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "z-ai/glm-5.3",
    "messages": [
      {"role": "user", "content": "What is the capital of France?"}
    ]
  }'
```

***

## Multi-turn Conversation

Include conversation history for context:

```bash
curl https://api.tensorx.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "z-ai/glm-5.3",
    "messages": [
      {"role": "user", "content": "My name is Alex"},
      {"role": "assistant", "content": "Nice to meet you, Alex! How can I help you today?"},
      {"role": "user", "content": "What is my name?"}
    ]
  }'
```

***

## Streaming Response

Get responses token-by-token for real-time output:

```bash
curl https://api.tensorx.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "z-ai/glm-5.3",
    "stream": true,
    "messages": [
      {"role": "user", "content": "Write a haiku about coding"}
    ]
  }'
```

***

## Code Generation

Ask for code with a system prompt:

```bash
curl https://api.tensorx.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "z-ai/glm-5.3",
    "messages": [
      {"role": "system", "content": "You are an expert Python developer. Write clean, well-documented code."},
      {"role": "user", "content": "Write a function to check if a string is a palindrome"}
    ]
  }'
```

***

## With Temperature Control

Adjust creativity with temperature (0.0 = focused, 1.0 = creative):

```bash
curl https://api.tensorx.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "z-ai/glm-5.3",
    "temperature": 0.7,
    "messages": [
      {"role": "user", "content": "Give me 3 creative startup ideas"}
    ]
  }'
```

***

## List Available Models

Check which models are available:

```bash
curl https://api.tensorx.ai/v1/models \
  -H "Authorization: Bearer YOUR_API_KEY"
```

***

## Python Example

Using the OpenAI Python SDK:

```python
import os
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.tensorx.ai/v1"
)

response = client.chat.completions.create(
    model=os.environ["TENSORX_MODEL"],
    messages=[
        {"role": "user", "content": "Hello, world!"}
    ]
)

print(response.choices[0].message.content)
```

***

## JavaScript/Node.js Example

Using the OpenAI Node.js SDK:

```javascript
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'YOUR_API_KEY',
  baseURL: 'https://api.tensorx.ai/v1'
});

const response = await client.chat.completions.create({
  model: process.env.TENSORX_MODEL,
  messages: [
    { role: 'user', content: 'Hello, world!' }
  ]
});

console.log(response.choices[0].message.content);
```

***

## Model metadata

Use `GET /v1/model/info` to inspect current capability flags and limits. See [Models](/api-reference/models.md) for the discovery commands and response fields.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.tensorx.ai/api-reference/api-examples.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
