sdk-examples.mdx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ---
  2. title: "Code Examples"
  3. sidebarTitle: "Code Examples"
  4. description: "Use the Cline API from Python, Node.js, curl, the Cline CLI, and the VS Code extension."
  5. ---
  6. 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.
  7. ## curl
  8. ### Non-Streaming
  9. ```bash
  10. curl -X POST https://api.cline.bot/api/v1/chat/completions \
  11. -H "Authorization: Bearer $CLINE_API_KEY" \
  12. -H "Content-Type: application/json" \
  13. -d '{
  14. "model": "anthropic/claude-sonnet-4-6",
  15. "messages": [{"role": "user", "content": "What is 2+2?"}],
  16. "stream": false
  17. }'
  18. ```
  19. ### Streaming
  20. ```bash
  21. curl -X POST https://api.cline.bot/api/v1/chat/completions \
  22. -H "Authorization: Bearer $CLINE_API_KEY" \
  23. -H "Content-Type: application/json" \
  24. -d '{
  25. "model": "anthropic/claude-sonnet-4-6",
  26. "messages": [{"role": "user", "content": "Write a short poem about code."}],
  27. "stream": true
  28. }'
  29. ```
  30. ## Python
  31. ### OpenAI SDK
  32. The [OpenAI Python SDK](https://github.com/openai/openai-python) works with the Cline API by setting `base_url`:
  33. ```python
  34. from openai import OpenAI
  35. client = OpenAI(
  36. base_url="https://api.cline.bot/api/v1",
  37. api_key="YOUR_API_KEY",
  38. )
  39. # Non-streaming
  40. response = client.chat.completions.create(
  41. model="anthropic/claude-sonnet-4-6",
  42. messages=[{"role": "user", "content": "Explain recursion in one sentence."}],
  43. )
  44. print(response.choices[0].message.content)
  45. ```
  46. ### Streaming in Python
  47. ```python
  48. from openai import OpenAI
  49. client = OpenAI(
  50. base_url="https://api.cline.bot/api/v1",
  51. api_key="YOUR_API_KEY",
  52. )
  53. stream = client.chat.completions.create(
  54. model="anthropic/claude-sonnet-4-6",
  55. messages=[{"role": "user", "content": "Write a function to reverse a string in Python."}],
  56. stream=True,
  57. )
  58. for chunk in stream:
  59. content = chunk.choices[0].delta.content
  60. if content:
  61. print(content, end="", flush=True)
  62. print()
  63. ```
  64. ### Tool Calling in Python
  65. ```python
  66. from openai import OpenAI
  67. import json
  68. client = OpenAI(
  69. base_url="https://api.cline.bot/api/v1",
  70. api_key="YOUR_API_KEY",
  71. )
  72. tools = [
  73. {
  74. "type": "function",
  75. "function": {
  76. "name": "get_weather",
  77. "description": "Get weather for a location",
  78. "parameters": {
  79. "type": "object",
  80. "properties": {
  81. "location": {"type": "string", "description": "City name"}
  82. },
  83. "required": ["location"],
  84. },
  85. },
  86. }
  87. ]
  88. response = client.chat.completions.create(
  89. model="anthropic/claude-sonnet-4-6",
  90. messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
  91. tools=tools,
  92. )
  93. # Check if the model wants to call a tool
  94. choice = response.choices[0]
  95. if choice.message.tool_calls:
  96. tool_call = choice.message.tool_calls[0]
  97. print(f"Tool: {tool_call.function.name}")
  98. print(f"Args: {tool_call.function.arguments}")
  99. ```
  100. ### Using requests
  101. If you prefer not to use the OpenAI SDK:
  102. ```python
  103. import requests
  104. response = requests.post(
  105. "https://api.cline.bot/api/v1/chat/completions",
  106. headers={
  107. "Authorization": "Bearer YOUR_API_KEY",
  108. "Content-Type": "application/json",
  109. },
  110. json={
  111. "model": "anthropic/claude-sonnet-4-6",
  112. "messages": [{"role": "user", "content": "Hello!"}],
  113. "stream": False,
  114. },
  115. )
  116. data = response.json()
  117. print(data["choices"][0]["message"]["content"])
  118. ```
  119. ## Node.js / TypeScript
  120. ### OpenAI SDK
  121. The [OpenAI Node.js SDK](https://github.com/openai/openai-node) works with the Cline API by setting `baseURL`:
  122. ```typescript
  123. import OpenAI from "openai"
  124. const client = new OpenAI({
  125. baseURL: "https://api.cline.bot/api/v1",
  126. apiKey: "YOUR_API_KEY",
  127. })
  128. // Non-streaming
  129. const response = await client.chat.completions.create({
  130. model: "anthropic/claude-sonnet-4-6",
  131. messages: [{ role: "user", content: "Explain async/await in one sentence." }],
  132. })
  133. console.log(response.choices[0].message.content)
  134. ```
  135. ### Streaming in Node.js
  136. ```typescript
  137. import OpenAI from "openai"
  138. const client = new OpenAI({
  139. baseURL: "https://api.cline.bot/api/v1",
  140. apiKey: "YOUR_API_KEY",
  141. })
  142. const stream = await client.chat.completions.create({
  143. model: "anthropic/claude-sonnet-4-6",
  144. messages: [{ role: "user", content: "Write a haiku about TypeScript." }],
  145. stream: true,
  146. })
  147. for await (const chunk of stream) {
  148. const content = chunk.choices[0]?.delta?.content
  149. if (content) {
  150. process.stdout.write(content)
  151. }
  152. }
  153. console.log()
  154. ```
  155. ### Using fetch
  156. ```typescript
  157. const response = await fetch("https://api.cline.bot/api/v1/chat/completions", {
  158. method: "POST",
  159. headers: {
  160. Authorization: "Bearer YOUR_API_KEY",
  161. "Content-Type": "application/json",
  162. },
  163. body: JSON.stringify({
  164. model: "anthropic/claude-sonnet-4-6",
  165. messages: [{ role: "user", content: "Hello!" }],
  166. stream: false,
  167. }),
  168. })
  169. const data = await response.json()
  170. console.log(data.choices[0].message.content)
  171. ```
  172. ## Cline CLI
  173. 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.
  174. ### Setup
  175. ```bash
  176. # Install
  177. npm install -g @anthropic-ai/cline
  178. # Authenticate with a Cline API key
  179. cline auth -p cline -k "YOUR_API_KEY" -m anthropic/claude-sonnet-4-6
  180. ```
  181. ### Run Tasks
  182. ```bash
  183. # Simple prompt
  184. cline "Explain what a REST API is."
  185. # Pipe input
  186. cat README.md | cline "Summarize this document."
  187. # Use a specific model
  188. cline -m google/gemini-2.5-pro "Analyze this codebase."
  189. # YOLO mode for automation
  190. cline -y "Run tests and fix failures."
  191. ```
  192. See the [CLI Reference](/cline-cli/cli-reference) for all commands and options.
  193. ## VS Code / JetBrains
  194. The Cline extension handles the API integration for you:
  195. 1. Open the Cline panel in your editor
  196. 2. Select **Cline** as the provider in the model picker
  197. 3. Sign in with your Cline account
  198. 4. Start chatting or give Cline a task
  199. Your API key is managed automatically. No manual configuration needed.
  200. For setup instructions, see [Installing Cline](/getting-started/installing-cline) and [Authorizing with Cline](/getting-started/authorizing-with-cline).
  201. ## Related
  202. <CardGroup cols={2}>
  203. <Card title="Chat Completions" icon="message" href="/api/chat-completions">
  204. Full endpoint reference with all parameters.
  205. </Card>
  206. <Card title="Authentication" icon="key" href="/api/authentication">
  207. API key management and security practices.
  208. </Card>
  209. <Card title="Models" icon="brain" href="/api/models">
  210. Browse available models.
  211. </Card>
  212. <Card title="CLI Reference" icon="terminal" href="/cline-cli/cli-reference">
  213. Complete Cline CLI command reference.
  214. </Card>
  215. </CardGroup>