> ## 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.