mirror of
https://github.com/cline/cline.git
synced 2026-09-19 02:05:44 +08:00
* docs: fix SDK documentation accuracy and completeness - Fix setPermissionHandler API Reference to use correct async/return signature (was showing old callback pattern with (request, resolve) => void) - Remove non-existent PermissionResolver type from Exported Types table - Fix PermissionHandler type description to match actual signature - Add missing hooksDir option to ClineAgentOptions documentation - Fix newSession() example to use real model IDs - Replace developer personal path in Full Example with generic path - Use placeholder for version in initialize() example to avoid staleness - Expand Stop Reasons table and add note about current implementation - Add missing key exported types: AcpSessionStatus, AcpSessionState, RequestPermissionRequest/Response, PermissionOption, SessionUpdatePayload, SessionModelState, ModelInfo, TextContent/ImageContent/AudioContent, SetSessionMode/Model request/response types, TranslatedMessage * docs: improve SDK visibility and disambiguate from API code examples - Move SDK page higher in Cline CLI nav (after Installation, before Interactive Mode) - Add sidebarTitle 'SDK (Programmatic Use)' for clearer nav label - Rename api/sdk-examples to 'Code Examples' to avoid naming confusion with the Cline SDK - Update API overview card title to match * Apply suggestions from code review Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * docs: add ClientCapabilities, Error Handling, and BYO API key docs to SDK - Document clientCapabilities object and its effect on agent behavior - Add Error Handling section with all throwable errors per method - Expand BYO API key setup with concrete CLI auth examples * docs: fix duplicated Stop Reasons table rows from code review * docs: fix 3 accuracy issues found in source code audit - Fix protocolVersion: was '0.9.0' (fabricated), actually 1 (number) from @agentclientprotocol/sdk - Fix clientCapabilities: was claiming they change SDK behavior, but ClineAgent always uses standalone providers (capabilities only matter via AcpAgent stdio wrapper) - Fix permission options: remove reject_always (never sent by agent, only allow_once/allow_always/reject_once are used) * docs: clarify custom clineDir usage with CLI --config flag Address PR review feedback: the BYO auth section mentioned custom clineDir without showing how to target it from the CLI. Remove the vague reference and add explicit --config flag documentation with side-by-side SDK and CLI examples. * docs: remove misleading 'by default' qualifier from SDK BYO auth section --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
276 lines
6.6 KiB
Plaintext
276 lines
6.6 KiB
Plaintext
---
|
|
title: "Code Examples"
|
|
sidebarTitle: "Code Examples"
|
|
description: "Use the Cline API from Python, Node.js, curl, the Cline CLI, and the VS Code extension."
|
|
---
|
|
|
|
The Cline API is OpenAI-compatible, so any library or tool that works with OpenAI also works with the Cline API. Just change the base URL and API key.
|
|
|
|
## curl
|
|
|
|
### Non-Streaming
|
|
|
|
```bash
|
|
curl -X POST https://api.cline.bot/api/v1/chat/completions \
|
|
-H "Authorization: Bearer $CLINE_API_KEY" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"model": "anthropic/claude-sonnet-4-6",
|
|
"messages": [{"role": "user", "content": "What is 2+2?"}],
|
|
"stream": false
|
|
}'
|
|
```
|
|
|
|
### Streaming
|
|
|
|
```bash
|
|
curl -X POST https://api.cline.bot/api/v1/chat/completions \
|
|
-H "Authorization: Bearer $CLINE_API_KEY" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"model": "anthropic/claude-sonnet-4-6",
|
|
"messages": [{"role": "user", "content": "Write a short poem about code."}],
|
|
"stream": true
|
|
}'
|
|
```
|
|
|
|
## Python
|
|
|
|
### OpenAI SDK
|
|
|
|
The [OpenAI Python SDK](https://github.com/openai/openai-python) works with the Cline API by setting `base_url`:
|
|
|
|
```python
|
|
from openai import OpenAI
|
|
|
|
client = OpenAI(
|
|
base_url="https://api.cline.bot/api/v1",
|
|
api_key="YOUR_API_KEY",
|
|
)
|
|
|
|
# Non-streaming
|
|
response = client.chat.completions.create(
|
|
model="anthropic/claude-sonnet-4-6",
|
|
messages=[{"role": "user", "content": "Explain recursion in one sentence."}],
|
|
)
|
|
print(response.choices[0].message.content)
|
|
```
|
|
|
|
### Streaming in Python
|
|
|
|
```python
|
|
from openai import OpenAI
|
|
|
|
client = OpenAI(
|
|
base_url="https://api.cline.bot/api/v1",
|
|
api_key="YOUR_API_KEY",
|
|
)
|
|
|
|
stream = client.chat.completions.create(
|
|
model="anthropic/claude-sonnet-4-6",
|
|
messages=[{"role": "user", "content": "Write a function to reverse a string in Python."}],
|
|
stream=True,
|
|
)
|
|
|
|
for chunk in stream:
|
|
content = chunk.choices[0].delta.content
|
|
if content:
|
|
print(content, end="", flush=True)
|
|
print()
|
|
```
|
|
|
|
### Tool Calling in Python
|
|
|
|
```python
|
|
from openai import OpenAI
|
|
import json
|
|
|
|
client = OpenAI(
|
|
base_url="https://api.cline.bot/api/v1",
|
|
api_key="YOUR_API_KEY",
|
|
)
|
|
|
|
tools = [
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_weather",
|
|
"description": "Get weather for a location",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {
|
|
"location": {"type": "string", "description": "City name"}
|
|
},
|
|
"required": ["location"],
|
|
},
|
|
},
|
|
}
|
|
]
|
|
|
|
response = client.chat.completions.create(
|
|
model="anthropic/claude-sonnet-4-6",
|
|
messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
|
|
tools=tools,
|
|
)
|
|
|
|
# Check if the model wants to call a tool
|
|
choice = response.choices[0]
|
|
if choice.message.tool_calls:
|
|
tool_call = choice.message.tool_calls[0]
|
|
print(f"Tool: {tool_call.function.name}")
|
|
print(f"Args: {tool_call.function.arguments}")
|
|
```
|
|
|
|
### Using requests
|
|
|
|
If you prefer not to use the OpenAI SDK:
|
|
|
|
```python
|
|
import requests
|
|
|
|
response = requests.post(
|
|
"https://api.cline.bot/api/v1/chat/completions",
|
|
headers={
|
|
"Authorization": "Bearer YOUR_API_KEY",
|
|
"Content-Type": "application/json",
|
|
},
|
|
json={
|
|
"model": "anthropic/claude-sonnet-4-6",
|
|
"messages": [{"role": "user", "content": "Hello!"}],
|
|
"stream": False,
|
|
},
|
|
)
|
|
|
|
data = response.json()
|
|
print(data["choices"][0]["message"]["content"])
|
|
```
|
|
|
|
## Node.js / TypeScript
|
|
|
|
### OpenAI SDK
|
|
|
|
The [OpenAI Node.js SDK](https://github.com/openai/openai-node) works with the Cline API by setting `baseURL`:
|
|
|
|
```typescript
|
|
import OpenAI from "openai"
|
|
|
|
const client = new OpenAI({
|
|
baseURL: "https://api.cline.bot/api/v1",
|
|
apiKey: "YOUR_API_KEY",
|
|
})
|
|
|
|
// Non-streaming
|
|
const response = await client.chat.completions.create({
|
|
model: "anthropic/claude-sonnet-4-6",
|
|
messages: [{ role: "user", content: "Explain async/await in one sentence." }],
|
|
})
|
|
console.log(response.choices[0].message.content)
|
|
```
|
|
|
|
### Streaming in Node.js
|
|
|
|
```typescript
|
|
import OpenAI from "openai"
|
|
|
|
const client = new OpenAI({
|
|
baseURL: "https://api.cline.bot/api/v1",
|
|
apiKey: "YOUR_API_KEY",
|
|
})
|
|
|
|
const stream = await client.chat.completions.create({
|
|
model: "anthropic/claude-sonnet-4-6",
|
|
messages: [{ role: "user", content: "Write a haiku about TypeScript." }],
|
|
stream: true,
|
|
})
|
|
|
|
for await (const chunk of stream) {
|
|
const content = chunk.choices[0]?.delta?.content
|
|
if (content) {
|
|
process.stdout.write(content)
|
|
}
|
|
}
|
|
console.log()
|
|
```
|
|
|
|
### Using fetch
|
|
|
|
```typescript
|
|
const response = await fetch("https://api.cline.bot/api/v1/chat/completions", {
|
|
method: "POST",
|
|
headers: {
|
|
Authorization: "Bearer YOUR_API_KEY",
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
model: "anthropic/claude-sonnet-4-6",
|
|
messages: [{ role: "user", content: "Hello!" }],
|
|
stream: false,
|
|
}),
|
|
})
|
|
|
|
const data = await response.json()
|
|
console.log(data.choices[0].message.content)
|
|
```
|
|
|
|
## Cline CLI
|
|
|
|
The [Cline CLI](/cline-cli/cli-reference) is the fastest way to use the Cline API from your terminal. It handles authentication, streaming, and tool execution for you.
|
|
|
|
### Setup
|
|
|
|
```bash
|
|
# Install
|
|
npm install -g @anthropic-ai/cline
|
|
|
|
# Authenticate with a Cline API key
|
|
cline auth -p cline -k "YOUR_API_KEY" -m anthropic/claude-sonnet-4-6
|
|
```
|
|
|
|
### Run Tasks
|
|
|
|
```bash
|
|
# Simple prompt
|
|
cline "Explain what a REST API is."
|
|
|
|
# Pipe input
|
|
cat README.md | cline "Summarize this document."
|
|
|
|
# Use a specific model
|
|
cline -m google/gemini-2.5-pro "Analyze this codebase."
|
|
|
|
# YOLO mode for automation
|
|
cline -y "Run tests and fix failures."
|
|
```
|
|
|
|
See the [CLI Reference](/cline-cli/cli-reference) for all commands and options.
|
|
|
|
## VS Code / JetBrains
|
|
|
|
The Cline extension handles the API integration for you:
|
|
|
|
1. Open the Cline panel in your editor
|
|
2. Select **Cline** as the provider in the model picker
|
|
3. Sign in with your Cline account
|
|
4. Start chatting or give Cline a task
|
|
|
|
Your API key is managed automatically. No manual configuration needed.
|
|
|
|
For setup instructions, see [Installing Cline](/getting-started/installing-cline) and [Authorizing with Cline](/getting-started/authorizing-with-cline).
|
|
|
|
## Related
|
|
|
|
<CardGroup cols={2}>
|
|
<Card title="Chat Completions" icon="message" href="/api/chat-completions">
|
|
Full endpoint reference with all parameters.
|
|
</Card>
|
|
<Card title="Authentication" icon="key" href="/api/authentication">
|
|
API key management and security practices.
|
|
</Card>
|
|
<Card title="Models" icon="brain" href="/api/models">
|
|
Browse available models.
|
|
</Card>
|
|
<Card title="CLI Reference" icon="terminal" href="/cline-cli/cli-reference">
|
|
Complete Cline CLI command reference.
|
|
</Card>
|
|
</CardGroup>
|