# Lovable API Key 填在哪：添加步骤和防泄露设置

> Lovable API Key 放在 Lovable 的 Secrets 里：添加步骤、四种密钥的区别、后端函数写法，以及泄露后怎么处理。

- Canonical HTML: https://omniakey.com/zh/blog/lovable-api-key
- Markdown: https://omniakey.com/zh/blog/lovable-api-key.md
- Published: 2026-11-02 (updated 2026-11-02)
- Author: Kiki, COO at OmniaKey
- Language: zh
- Category: API setup
- Reading time: 10 min
- All posts: https://omniakey.com/blog/llms.txt

---

在 Lovable 里，Lovable API Key 放在 Secrets。项目工具栏的入口是 More → Settings → Secrets，也可以直接用项目聊天里弹出的 Add secret 表单。后端代码从那里读取，密钥不会进浏览器。把密钥贴进聊天消息、写进前端代码，都会让它暴露在页面上。

## 一句话答案

- **你自己的密钥**（GPT、Claude、Resend）：放 Secrets，或者用连接器给出的表单。
- **Lovable 自己的密钥**：`LOVABLE_API_KEY` 由系统生成；工作区密钥（`lov_…`）需要 Business 或 Enterprise 套餐。
- **用 GPT、Claude 或 DeepSeek 配自己的密钥**：让 Lovable 写一个后端函数，内置 AI 连接器读不到你的密钥。
- **密钥泄露**：先在提供商那边吊销，再替换 Secrets 里的值。

## 你要的是哪一种 Lovable API Key？

Lovable API Key 这个说法，对应四种不同的东西。

|你想做的事|密钥|从哪里来|
|-|-|-|
|调用 GPT、Resend 或其他付费服务|该服务发给你的密钥|Secrets，或连接器表单|
|使用 Lovable 内置 AI|`LOVABLE_API_KEY`|Lovable 按项目生成|
|通过 Lovable API 管理项目|工作区密钥（`lov_…`）|Workspace settings → API keys & OAuth → Workspace access tokens → New API key，Business 或 Enterprise，owner 或 admin|
|使用 MCP 服务器或 Build with URL|不需要 API 密钥|MCP 走 OAuth；Build with URL 什么都不用|

工作区密钥只发给 Lovable API，放在 `Lovable-API-Key` 请求头里。值只显示一次（[API 密钥文档](https://docs.lovable.dev/features/api-keys)，2026 年 11 月 2 日核对）。它负责管理和部署项目，不调用 AI 模型，也不生成应用（[Lovable API](https://docs.lovable.dev/integrations/lovable-api)），和项目里的 `LOVABLE_API_KEY` 是两回事（[AI 文档](https://docs.lovable.dev/features/ai)）。

下面说的都是第一行：你自己的密钥。

## 在 Lovable 里怎么添加 API 密钥

1. Lovable 会主动来问。某个功能需要密钥时，项目聊天里会打开 Add secret 表单。值填进表单，别发成聊天消息。表单直接把它存进项目的 Secrets，聊天记录里不留痕迹（[Lovable 的自定义 API 文档](https://docs.lovable.dev/integrations/any-api)，2026 年 11 月 2 日核对）。
2. 自己动手加。在项目工具栏打开 More → Settings → Secrets，点 Add secret。填一组或多组名称和值（[Secrets 文档](https://docs.lovable.dev/features/secrets)）。

Lovable 2026 年 8 月 26 日的更新日志把这个入口写成 More → Cloud → Secrets。菜单不一样就去那里找（[更新日志](https://docs.lovable.dev/changelog)）。

几条容易踩的规则：

- 只写不读：值可以替换或删除，读不回来。
- `VITE_` 开头的名称会被拒绝；这类值在构建时打包，浏览器看得到，应该放进 `.env`。
- `SUPABASE_` 和 `LOVABLE_` 开头的名称是保留的，系统替你填好。
- Secrets 属于单个项目。remix 之后要重新填自己的密钥，只有 Lovable 生成的密钥会自动重建。

用自己的 Supabase 项目时，后端视图里有 Manage secrets 按钮，点开是 Supabase 控制台。密钥存在 Edge Function Secrets（[Supabase secrets](https://supabase.com/docs/guides/functions/secrets)，2026 年 11 月 2 日核对）。两个 Lovable 项目共用一个 Supabase 项目时，会互相覆盖 Secrets。`LOVABLE_API_KEY` 也一起被覆盖（[Supabase 集成](https://docs.lovable.dev/integrations/supabase)）。

## Resend、Stripe、Google Maps 的密钥放在哪？

不是每个密钥都直接进 Secrets。这几个服务有连接器表单（[Stripe](https://docs.lovable.dev/integrations/stripe)、[Resend](https://docs.lovable.dev/integrations/resend)、[自定义 API](https://docs.lovable.dev/integrations/any-api)，2026 年 11 月 2 日核对）。

|服务|密钥放在哪|需要知道的|
|-|-|-|
|Stripe|项目聊天里的 Connect Stripe 表单|别把密钥发成聊天消息|
|Resend|连接表单|密钥以 `re_` 开头|
|Google Maps|连接表单里的两个密钥|服务器密钥保密；浏览器密钥本来就公开|
|其他服务|自定义连接器，或者放进 Secrets|服务端代码读取这个密钥|

Google Maps 是这条规则的例外。它的浏览器密钥本来就公开，以 `VITE_LOVABLE_CONNECTOR_GOOGLE_MAPS_BROWSER_KEY` 的形式进到前端。网站限制挡住别的站点复用它（[Google Maps](https://docs.lovable.dev/integrations/google-maps)，2026 年 11 月 2 日核对）。服务器密钥保持私密。连接器凭据加密保存，谁都读不回来，Lovable 也读不到（[集成安全](https://docs.lovable.dev/integrations/security)）。

## 用自己的密钥接 GPT、Claude 或 DeepSeek

先确认要不要密钥：内置 AI 连接器一个人都不用填。Lovable 按项目生成 `LOVABLE_API_KEY`，默认跑 [GPT-6 Astra](https://omniakey.com/zh/gpt-6-astra)。Claude 聊天模型只出现在 2026 年 5 月 13 日之后新建的 TanStack Start 应用里（[AI 文档](https://docs.lovable.dev/features/ai)、[AI chat](https://docs.lovable.dev/features/ai-chat)，2026 年 11 月 2 日核对）。

这个连接器用不了你的密钥：它始终走 Lovable，扣的是工作区额度。Lovable 的文档在 2026 年 11 月 2 日这一版里没有提到 DeepSeek。要用自己的密钥，就让 Lovable 写一个后端函数，从 Secrets 读密钥去调提供商的接口。

没有“新建函数”按钮：在项目聊天里把这个函数描述清楚，Lovable 会写好并部署（[Edge functions](https://docs.lovable.dev/features/edge-functions)）。2026 年 5 月 13 日之后新建的 TanStack Start 应用（Enterprise 是 6 月 22 日之后）得到 server function。更早的 React + Vite 应用得到 Edge Function，也就是在服务器上跑的代码。

下面的代码用 OmniaKey 的 [OpenAI 兼容 API](https://docs.omniakey.com/cn/api/openai-compatible)。一个密钥就能调用 [GPT-6.1 Terra](https://omniakey.com/zh/gpt-6-1-terra)、[Claude Sonnet 5.5](https://omniakey.com/zh/claude-sonnet-5-5) 和 [DeepSeek](https://omniakey.com/zh/deepseek-flash)。

1. 在提供商那边创建密钥。用 OmniaKey：先在 omniakey.com 注册，然后 Dashboard → tokens（[快速开始](https://docs.omniakey.com/cn/quick-start)）。

2. 把这段提示词粘进项目聊天：

   ```text
   Add an AI chat feature that uses my own API key, not the built-in AI connector.
   Create a backend function named "chat". It reads OMNIAKEY_API_KEY from Secrets and calls
   POST https://api.omniakey.com/v1/chat/completions with the model and messages the frontend sends.
   Only allow these models: gpt-6.1-terra, claude-sonnet-5-5, deepseek-flash.
   Require a signed-in user. Never put the key in frontend code.
   Ask me for the key with the Add secret form.
   ```

3. 在 Add secret 表单里填这个密钥，名称写 `OMNIAKEY_API_KEY`。

我们没有 Lovable 账号，这段提示词没在 Lovable 里跑过，Lovable 的步骤来自它的文档。函数最后应该长成下面这样，这段代码是我们在自己机器上跑的那一份。

```ts
// supabase/functions/chat/index.ts
// 浏览器发来 { model, messages }；这个函数在服务器上把 API 密钥加上。
const corsHeaders = {
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type",
};

const ALLOWED_MODELS = ["gpt-6.1-terra", "claude-sonnet-5-5", "deepseek-flash"];

function json(body: unknown, status = 200) {
  return new Response(JSON.stringify(body), {
    status,
    headers: { ...corsHeaders, "Content-Type": "application/json" },
  });
}

Deno.serve(async (req) => {
  if (req.method === "OPTIONS") {
    return new Response("ok", { headers: corsHeaders });
  }

  const apiKey = Deno.env.get("OMNIAKEY_API_KEY");
  if (!apiKey) {
    return json({ error: "OMNIAKEY_API_KEY is not set" }, 500);
  }

  const { model, messages } = await req.json();
  if (!ALLOWED_MODELS.includes(model)) {
    return json({ error: `model must be one of: ${ALLOWED_MODELS.join(", ")}` }, 400);
  }

  const upstream = await fetch("https://api.omniakey.com/v1/chat/completions", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${apiKey}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ model, messages, max_tokens: 800 }),
  });

  const data = await upstream.json();
  if (!upstream.ok) {
    return json({ error: data.error?.message ?? "upstream error" }, upstream.status);
  }
  return json({ reply: data.choices[0].message.content, model: data.model, usage: data.usage });
});
```

测试用的版本去掉了登录校验，方便从脚本里直接调用。上面的提示词让 Lovable 把它加上。Lovable 的安全最佳实践要求鉴权判断放在服务器上（[best practices](https://docs.lovable.dev/tips-tricks/security-best-practices)）。

从浏览器调用是这样（[supabase-js](https://supabase.com/docs/reference/javascript/functions-invoke)）：

```ts
const { data, error } = await supabase.functions.invoke("chat", {
  body: { model: "deepseek-flash", messages: [{ role: "user", content: "Say hello in five words." }] },
});
```

换别家提供商时，把 URL 和模型 ID 换成它文档里的值。OmniaKey 的密钥也能用在 [Cline 的自定义 API 密钥](https://omniakey.com/zh/blog/cline-custom-api-key)和 [Factory Droid 的自定义模型](https://omniakey.com/zh/blog/factory-droid-custom-models)里。

## 跑起来的结果

2026 年 11 月 2 日，我们在自己的机器上用 Deno 2.9.8 跑这个函数。调用方式按浏览器来：带 Origin 请求头，先走一次 CORS 预检。

|测试|状态码|返回内容|
|-|-|-|
|预检（`OPTIONS`）|200|`*` 和允许的请求头列表|
|`gpt-6.1-terra`|200，3.28 s|一条回复，共 53 个 token|
|`claude-sonnet-5-5`|200，2.85 s|一条回复，共 134 个 token|
|`deepseek-flash`|200，1.75 s|一条回复，共 92 个 token|
|白名单外的模型|400|`model must be one of: gpt-6.1-terra, claude-sonnet-5-5, deepseek-flash`|
|缺密钥|500|`OMNIAKEY_API_KEY is not set`|
|密钥错误|401|`Invalid token (request id: …)`|

每个模型只调了一次，每次都是一个样本；`deepseek-flash` 在回复里报的模型名是 `deepseek-v4-1-flash-261017`。

### 为什么只看到 "non-2xx status code"

出错时，`supabase.functions.invoke` 只给你一句 `Edge Function returned a non-2xx status code`，返回的数据是 null。函数真正的报错在 `await error.context.json()` 里：

```ts
if (error) {
  const details = await error.context.json();
  console.error(details.error);
}
```

- 401：密钥不对。我们拿到的是 `Invalid token (request id: …)`。
- 500 加上 `OMNIAKEY_API_KEY is not set`：密钥没填，或者存在别的名称下。
- 400：模型不在你的白名单里。

## 怎么检查密钥有没有暴露

前端代码里的密钥用户都看得到，按已泄露处理（[best practices](https://docs.lovable.dev/tips-tricks/security-best-practices)，2026 年 11 月 2 日核对）。

- Quick security scan 在每次发布时自动跑。
- Deep security scan 不自动跑。要从项目的 Security view 手动开始（[Security view](https://docs.lovable.dev/features/security-view)）。
- 看 Leaked secrets & credentials 这一组。
- 两种扫描都免费；只有批量修复会先用掉 10 次免费额度，之后扣额度（[security scans](https://docs.lovable.dev/features/security)）。

手动查：在 Chrome 里打开已发布应用的开发者工具，按 Command+Option+F（Mac）或 Control+Shift+F（Windows、Linux）。在加载的文件里搜密钥开头那几个字符。看到 `VITE_` 开头的值里装着私钥就要处理，这类值本来就是打包给浏览器的（[DevTools search](https://developer.chrome.com/docs/devtools/search)）。搜索干净只覆盖当前页面加载的文件，扫描还是要跑一遍。

把密钥藏好，陌生人照样能调你的函数：没有登录校验的函数谁都能调，每次调用都花你的密钥。Lovable 的文档要求在服务器上做登录校验。我们的白名单和 `max_tokens: 800` 限制每次调用。OmniaKey 的文档建议每个环境用一把单独的密钥，并设置消费上限（[OmniaKey 的 API 文档](https://docs.omniakey.com/cn/api/authentication)）。

## 密钥已经泄露怎么办

1. 在签发它的服务里吊销，再新建一个；Lovable 的文档只给自己的密钥提供 Rotate 操作（[Secrets 文档](https://docs.lovable.dev/features/secrets)）。
2. 在 Secrets、连接器表单或 Supabase 里换掉这个值。在 Supabase 上，函数马上读到新密钥，不用重新部署（[Supabase secrets](https://supabase.com/docs/guides/functions/secrets)）。Lovable 的内置后端 Cloud 是不是也这样，文档没有写，调一次函数就能确认。
3. 把密钥从前端代码或 `.env` 里移走，再跑一次 Deep security scan。
4. 去提供商的用量页面看，有没有不是你发出的调用。

Lovable 自己的密钥规则不同。`LOVABLE_API_KEY` 有 Rotate 操作，editor 及以上可用，会签发新密钥并更新 Edge Function 的环境。GitHub secret scanning 报告的工作区密钥（`lov_…`）会被自动吊销；出现在别处就自己吊销（[API 密钥文档](https://docs.lovable.dev/features/api-keys)）。

## 费用怎么算

Lovable 的账单和提供商的账单分开。用自己的密钥时，模型用量由提供商计费。应用只用 Cloud 来跑这个函数（[AI 文档](https://docs.lovable.dev/features/ai)、[Cloud](https://docs.lovable.dev/features/cloud)，2026 年 11 月 2 日核对）。

内置 AI 连接器扣的是工作区额度。Free、Pro 和 Business 每月有 4-credit 的 AI 额度，用完返回 402，超过速率限制返回 429。Free 套餐每天还给 5 build credits，每月最多 30，外加 20 Cloud credits（[价格页](https://lovable.dev/pricing)）。Lovable 的套餐表格里，Pro 每月 $25 给 100 credits（[订阅套餐](https://docs.lovable.dev/introduction/subscription-plans)，2026 年 11 月 2 日核对）。

OmniaKey 的当前价格见 [GPT-6.1 Terra](https://omniakey.com/zh/gpt-6-1-terra)、[Claude Sonnet 5.5](https://omniakey.com/zh/claude-sonnet-5-5) 和 [DeepSeek](https://omniakey.com/zh/deepseek-flash) 页面。

## 常见问题

### Lovable API 密钥在哪里找？

要看你指的是哪一种。`LOVABLE_API_KEY` 按项目生成，就在那个项目的 Secrets 里，可以轮换，读不到明文。工作区密钥在 Workspace settings → API keys & OAuth → Workspace access tokens 里生成。只显示一次。

### 有免费的 Lovable API Key 吗？

最接近的是 `LOVABLE_API_KEY`，内置 AI 连接器为每个项目生成它。Free、Pro 和 Business 工作区每月有 4-credit 的 AI 额度可以用它。工作区密钥要 Business 或 Enterprise 套餐。

### 能把我的 OpenAI 密钥用在 Lovable AI 上吗？

内置 AI 连接器不行，它始终走 Lovable，扣工作区额度。要用自己的密钥，就让 Lovable 写一个后端函数直接调提供商，密钥放在 Secrets。

### API 密钥该放 Secrets 还是 .env？

密钥私密、由后端代码读取时放 Secrets。`.env` 放 `VITE_` 开头的值，它们会打包进应用，浏览器里看得到。

### Lovable 接不上 GPT、Claude 或 DeepSeek 是怎么回事？

先看函数返回的报错，它会指出问题：

- 500，报 `OMNIAKEY_API_KEY is not set`：密钥没填，或者存在别的名称下。
- 401：密钥不对，或者已经吊销。
- 400：模型 ID 不在白名单里。
- 404：base URL 不对；我们的地址到 `/v1/chat/completions` 为止，多数配置错误出在协议后缀写错（[OmniaKey 的错误文档](https://docs.omniakey.com/cn/api/errors)）。

浏览器里只看到 `Edge Function returned a non-2xx status code`，按上面的写法把 `error.context` 打出来，才知道是哪一个。

## 参考来源

- [Lovable 的 Secrets 文档](https://docs.lovable.dev/features/secrets)，2026 年 11 月 2 日核对
- [Lovable API 密钥](https://docs.lovable.dev/features/api-keys)，2026 年 11 月 2 日核对
- [Lovable API](https://docs.lovable.dev/integrations/lovable-api)，2026 年 11 月 2 日核对
- [Lovable MCP 服务器](https://docs.lovable.dev/integrations/lovable-mcp-server)，2026 年 11 月 2 日核对
- [Build with URL](https://docs.lovable.dev/integrations/build-with-url)，2026 年 11 月 2 日核对
- [Lovable AI 功能](https://docs.lovable.dev/features/ai)，2026 年 11 月 2 日核对
- [Lovable AI 聊天](https://docs.lovable.dev/features/ai-chat)，2026 年 11 月 2 日核对
- [Lovable Edge functions](https://docs.lovable.dev/features/edge-functions)，2026 年 11 月 2 日核对
- [Lovable Cloud](https://docs.lovable.dev/features/cloud)，2026 年 11 月 2 日核对
- [自定义 API 和连接器](https://docs.lovable.dev/integrations/any-api)，2026 年 11 月 2 日核对
- [Stripe 集成](https://docs.lovable.dev/integrations/stripe)，2026 年 11 月 2 日核对
- [Resend 集成](https://docs.lovable.dev/integrations/resend)，2026 年 11 月 2 日核对
- [Google Maps 集成](https://docs.lovable.dev/integrations/google-maps)，2026 年 11 月 2 日核对
- [集成安全](https://docs.lovable.dev/integrations/security)，2026 年 11 月 2 日核对
- [Supabase 集成](https://docs.lovable.dev/integrations/supabase)，2026 年 11 月 2 日核对
- [Lovable 安全](https://docs.lovable.dev/features/security)，2026 年 11 月 2 日核对
- [Lovable Security view](https://docs.lovable.dev/features/security-view)，2026 年 11 月 2 日核对
- [Lovable 安全最佳实践](https://docs.lovable.dev/tips-tricks/security-best-practices)，2026 年 11 月 2 日核对
- [Lovable 订阅套餐](https://docs.lovable.dev/introduction/subscription-plans)，2026 年 11 月 2 日核对
- [Lovable 更新日志](https://docs.lovable.dev/changelog)，2026 年 11 月 2 日核对
- [Lovable 价格页](https://lovable.dev/pricing)，2026 年 11 月 2 日核对
- [Supabase Edge Function secrets](https://supabase.com/docs/guides/functions/secrets)，2026 年 11 月 2 日核对
- [Supabase functions.invoke](https://supabase.com/docs/reference/javascript/functions-invoke)，2026 年 11 月 2 日核对
- [Chrome DevTools 搜索](https://developer.chrome.com/docs/devtools/search)，2026 年 11 月 2 日核对

OmniaKey 自己的文档，2026 年 11 月 2 日核对：[快速开始](https://docs.omniakey.com/cn/quick-start)、[OpenAI 兼容 API](https://docs.omniakey.com/cn/api/openai-compatible)、[认证](https://docs.omniakey.com/cn/api/authentication) 和 [错误](https://docs.omniakey.com/cn/api/errors)。
