> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hitheo.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 生成代碼

> 使用 Theo Code 引擎生成生產級代碼。

根據自然語言提示生成代碼。Theo Code 引擎針對生產級代碼和長篇輸出進行了優化。

## 身份驗證

需要 Bearer 令牌。參見[身份驗證](/api-reference/authentication)。

## 請求正文

<ParamField body="prompt" type="string" required>
  待生成代碼的描述。
</ParamField>

<ParamField body="language" type="string">
  目標編程語言(例如 `"typescript"`、`"python"`、`"go"`、`"rust"`)。作為提示追加給引擎。
</ParamField>

<ParamField body="framework" type="string">
  目標框架(例如 `"express"`、`"nextjs"`、`"fastapi"`、`"gin"`)。作為提示追加給引擎。
</ParamField>

## 請求示例

<CodeGroup>
  ```bash curl theme={null}
  curl -X POST https://www.hitheo.ai/api/v1/code \
    -H "Authorization: Bearer $THEO_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "prompt": "Write a TypeScript Express middleware for JWT authentication",
      "language": "typescript",
      "framework": "express"
    }'
  ```

  ```typescript SDK theme={null}
  const res = await theo.code({
    prompt: "Write a TypeScript Express middleware for JWT authentication",
    language: "typescript",
    framework: "express",
  });

  console.log(res.content);    // Generated code
  console.log(res.artifacts);  // Any files created
  ```
</CodeGroup>

## 響應

<ResponseField name="id" type="string">
  唯一的代碼生成 ID(以 `code_` 為前綴)。
</ResponseField>

<ResponseField name="created" type="string">
  ISO 8601 時間戳。
</ResponseField>

<ResponseField name="content" type="string">
  以文本形式返回的生成代碼。
</ResponseField>

<ResponseField name="artifacts" type="object[]">
  生成過程中創建的任何文件(例如多文件輸出)。
</ResponseField>

<ResponseField name="tools_used" type="object[]">
  代碼生成過程中調用的工具。

  <Expandable title="Tool usage object">
    <ResponseField name="name" type="string">工具名稱。</ResponseField>
    <ResponseField name="status" type="string">`"success"` 或 `"error"`。</ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="usage" type="object">
  <Expandable title="Usage object">
    <ResponseField name="cost_cents" type="number">以美分為單位的費用。</ResponseField>
  </Expandable>
</ResponseField>

### 響應示例

```json theme={null}
{
  "id": "code_abc123",
  "created": "2026-04-10T12:00:00Z",
  "content": "import { Request, Response, NextFunction } from 'express';\nimport jwt from 'jsonwebtoken';\n\nexport function authMiddleware(req: Request, res: Response, next: NextFunction) {\n  const token = req.headers.authorization?.split(' ')[1];\n  if (!token) return res.status(401).json({ error: 'No token provided' });\n  try {\n    const decoded = jwt.verify(token, process.env.JWT_SECRET!);\n    req.user = decoded;\n    next();\n  } catch {\n    return res.status(401).json({ error: 'Invalid token' });\n  }\n}",
  "artifacts": [],
  "tools_used": [],
  "usage": { "cost_cents": 0.05 }
}
```

## 錯誤

| 狀態碼 | 代碼 | 描述 |
| - | - | - |
| 400 | `missing_prompt` | 缺少必需的 `prompt` |
| 401 | `invalid_api_key` | API 密鑰缺失或無效 |
| 429 | `rate_limit_exceeded` | 請求過多 |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.