这是一个带有API key使用统计功能的API代理服务,可以转发请求到指定的目标域名并跟踪API key的使用情况。
- API请求转发: 将请求转发到配置的目标域名
- API Key管理: 跟踪每个API key的使用统计
- 使用限制: 当API key剩余次数为0时拒绝请求
- 统计记录: 记录总调用次数、成功次数、剩余次数
- 管理接口: 提供API key的创建、查询和更新功能
- Next.js 15: React框架
- Drizzle ORM: 数据库ORM
- PostgreSQL: 数据库
- TypeScript: 类型安全
- 安装依赖:
npm install- 创建环境变量文件
.env.local:
# Database
DATABASE_URL="postgresql://username:password@localhost:5432/felix_db"
# Target API domain for forwarding requests
TARGET_API_DOMAIN="https://api.example.com"
# API key for the target service (if needed)
TARGET_API_KEY="your_target_api_key"- 生成数据库迁移:
npm run db:generate- 运行数据库迁移:
npm run db:migrate- 启动开发服务器:
npm run dev转发请求到目标域名的generate接口。
请求参数:
{
"apiKey": "your_api_key",
"prompt": "your prompt",
"width": 512,
"height": 512,
"steps": 20,
"batch_size": 1,
"model": "default"
}响应:
- 成功: 返回目标API的响应数据
- 失败: 返回错误信息
GET /api/apikey?apiKey=your_api_key
POST /api/apikey
{
"apiKey": "new_api_key",
"initialRemainingCalls": 100
}PUT /api/apikey
{
"apiKey": "your_api_key",
"remainingCalls": 50
}id: 主键apiKey: API key (唯一)totalCalls: 总调用次数successfulCalls: 成功调用次数remainingCalls: 剩余调用次数createdAt: 创建时间updatedAt: 更新时间
id: 主键totalGenerations: 总生成次数dailyGenerations: 每日生成次数createdAt: 创建时间updatedAt: 更新时间
- 创建API Key: 使用
/api/apikeyPOST接口创建新的API key(必需) - 调用Generate API: 使用有效的API key调用
/api/generate接口 - 查看统计: 使用
/api/apikeyGET接口查看使用统计 - 管理配额: 使用
/api/apikeyPUT接口更新剩余次数
注意: API key必须先通过管理接口创建,无效的API key会导致401错误。
访问 http://localhost:3000/test 可以使用Web界面测试所有功能。
npm run dev: 启动开发服务器npm run build: 构建生产版本npm run start: 启动生产服务器npm run db:generate: 生成数据库迁移npm run db:migrate: 运行数据库迁移npm run db:studio: 启动Drizzle Studio
- 确保PostgreSQL数据库已正确配置
- 设置正确的目标域名环境变量
- API key必须先通过管理接口创建,不会自动创建
- 当剩余次数为0时,请求会被拒绝
- 只有成功的请求才会减少剩余次数
- 无效的API key会返回401错误
This is a Next.js project bootstrapped with create-next-app.
First, run the development server:
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun devOpen http://localhost:3000 with your browser to see the result.
You can start editing the page by modifying app/page.tsx. The page auto-updates as you edit the file.
This project uses next/font to automatically optimize and load Geist, a new font family for Vercel.
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your feedback and contributions are welcome!
The easiest way to deploy your Next.js app is to use the Vercel Platform from the creators of Next.js.
Check out our Next.js deployment documentation for more details.