@genesis-xyz/ai

中文文档
0.0.8发布于 2024/01/25MITTypeScriptES Module近 30 天下载 1.2万

Auth that enables users to sign in with their own OpenAI API key, so that you can build apps powered by users. Your app, their API usage.

原始使用文档
文档目录6 节
  1. Sign In With AI
  2. Why build apps using Sign In With AI?
  3. Installation
  4. Usage
  5. FAQ
  6. For users: How do I get an OpenAI API key?

Sign In With AI

Auth that enables users to sign in with their own OpenAI API key, so that you can build apps powered by users. Your app, their API usage.

User OpenAI API keys will not be shared with your app. Instead, your app will receive an apiKey and baseURL for a proxy to the OpenAI API which uses your user's API keys.

Why build apps using Sign In With AI?

Because we believe it should be easier for developers to experiment and build new ideas for AI apps.

For each new app they build, developers also become responsible for the cost of using the OpenAI API and apps that leverage cutting-edge models like GPT-4 can carry a large bill if usage grows. Multiply that cost burden across several app ideas and that's a lot of overhead, making it harder to justify building new app ideas.

As a result, it’s becoming increasingly common to allow users to paste their personal OpenAI API key directly into the app. Not only is this bad security, it’s also super annoying for users to manually paste their API key in every new app that they want to try.

By offering a way for users to sign in with their own OpenAI API key, Genesis provides a safer way for users to try more AI apps and empowers developers to build their app ideas more easily, without taking on all of the cost burden for compute. It’s a win-win for everyone involved.

Installation

bun add @genesis-xyz/ai

‼️ Add this script to the <head> tag of your site:

<script src="https://unpkg.com/@passes/polyfill@^0.1.5"></script>

Usage

requestOpenAIAPI returns the following type, which may be passed directly to the OpenAI sdk constructor.

type RequestOpenAIAPIResult = {
  apiKey: string;
  baseURL: string;
};

With the openai sdk:

import { requestOpenAIAPI } from '@genesis-xyz/ai';
import { OpenAI } from 'openai';

// Request the user to "Sign In With OpenAI"
const openai = new OpenAI(await requestOpenAIAPI());

const chat = await openai.chat.completions.create({
  model: 'gpt-4',
  stream: true,
  messages: [{ role: 'user', content: 'Hello OpenAI!' }],
});

With client-side fetch:

import { requestOpenAIAPI } from '@genesis-xyz/ai';
import { OpenAI } from 'openai';

// Request the user to "Sign In With OpenAI"
const { baseURL, apiKey } = await requestOpenAIAPI();

// Use fetch to request the OpenAI API
const result = await fetch(`${baseURL}/chat/completions`, {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'gpt-4',
    messages: [{ role: 'user', content: 'Hello OpenAI!' }],
  }),
})

FAQ

How does it work?

Requesting an OpenAI API key from the user

  1. When a user first signs up for your app, a new tab opens and requests the user to enter their OpenAI API key and secure it with a Passkey.
  2. The API key is encrypted and stored for easy future access.
  3. Your app gets an apiKey and baseURL that it can use to configure the openai SDK.

Proxying the the OpenAI API

  1. Your app sends requests to the provided baseURL, which is a proxy to OpenAI's API.
  2. The proxy sends requests to OpenAI with the user's OpenAI API key.
  3. Responses from OpenAI are streamed to your app.
How is user data secured?
  • Encrypted API keys. User API keys are wrapped in (encrypted) JWTs for both storage and transmission, meaning the key is never directly exposed to the requesting site or transmitted over the network in plain text.
  • Reverse proxy. Genesis hosts a secure intermediary layer for proxying OpenAI API requests, which handles decrypting the user's API key, and forwarding the request to OpenAI. The reverse proxy allows the client to use the full functionality of the OpenAI API (including streaming) without ever handling the user API key.

For users: How do I get an OpenAI API key?

For now, it’s a manual process:

  1. If you already have an OpenAI account, sign in on the developer platform. If not, sign up for an account here.

    oai-dev-platform
  2. To get an API key, click the lock icon in the left-side toolbar.

    dev-platform-sidebar-toggle
  3. You’ll see all of the API keys that you’ve created on this page. To create a new API key, select Create new secret key, give it a name, and then copy the API key that appears.

    • Note: any previously created API keys can’t be retrieved from this page, so be sure to save the key when you create it.
    api-key-list
  4. Paste the API key in the text field and approve the request.

    genesis-request

版本与发布记录

@genesis-xyz/ai 共发布 8 个版本,首次发布于 2024/01/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.8latest2024/01/252 年前
0.0.72024/01/252 年前
0.0.62024/01/242 年前
0.0.52024/01/212 年前
0.0.42024/01/202 年前
0.0.32024/01/192 年前

关于 @genesis-xyz/ai 的常见问题

@genesis-xyz/ai 是什么?

Auth that enables users to sign in with their own OpenAI API key, so that you can build apps powered by users. Your app, their API usage.

如何安装 @genesis-xyz/ai?

在项目目录运行 npm install @genesis-xyz/ai,或使用 pnpm add / yarn add / bun add。

@genesis-xyz/ai 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@genesis-xyz/ai 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@genesis-xyz/ai 可以商用吗?

@genesis-xyz/ai 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@genesis-xyz/ai 的下载量有多少?

根据 npm 官方下载统计,@genesis-xyz/ai 在 2026/10/16 – 2026/11/14 这 30 天内被下载 11,988 次,日均约 400 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@genesis-xyz/ai 还在维护吗?

最新版本 0.0.8 发布于 2024/01/25(2 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/01/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。