Реализация продажи API-доступа (по ключу/подписке) на сайте
Монетизация API — продажа доступа к вашим данным или возможностям сторонним разработчикам. Покупатель получает API-ключ с квотой запросов и методами, соответствующими выбранному тарифу.
Структура API-ключей и тарифов
CREATE TABLE api_plans (
id SERIAL PRIMARY KEY,
name TEXT,
requests_per_month INTEGER, -- -1 = unlimited
requests_per_minute INTEGER,
endpoints JSONB, -- ['GET /v1/products', 'GET /v1/orders']
price_monthly NUMERIC(10,2),
);
CREATE TABLE api_keys (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT REFERENCES users(id),
plan_id INTEGER REFERENCES api_plans(id),
key_hash TEXT UNIQUE, -- bcrypt hash ключа
key_prefix CHAR(8), -- первые 8 символов для отображения
status TEXT, -- active, revoked, expired
expires_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE TABLE api_usage (
id BIGSERIAL PRIMARY KEY,
api_key_id BIGINT,
endpoint TEXT,
method TEXT,
status_code SMALLINT,
response_ms INTEGER,
created_at TIMESTAMPTZ DEFAULT NOW()
);
Генерация и хранение ключей
class ApiKeyService
{
public function generate(int $userId, int $planId): array
{
$rawKey = 'sk_' . Str::random(48); // Пример: sk_A1B2C3D4...
ApiKey::create([
'user_id' => $userId,
'plan_id' => $planId,
'key_hash' => Hash::make($rawKey),
'key_prefix' => substr($rawKey, 0, 8),
'status' => 'active',
]);
// Ключ показывается пользователю ОДИН РАЗ — после этого только хеш
return ['key' => $rawKey, 'prefix' => substr($rawKey, 0, 8)];
}
}
Middleware аутентификации API
class ApiKeyAuthMiddleware
{
public function handle(Request $request, Closure $next): Response
{
$rawKey = $request->header('X-API-Key')
?? $request->bearerToken()
?? $request->query('api_key');
if (!$rawKey) {
return response()->json(['error' => 'API key required'], 401);
}
// Быстрый поиск по префиксу, затем проверка хеша
$prefix = substr($rawKey, 0, 8);
$apiKey = ApiKey::where('key_prefix', $prefix)->where('status', 'active')->first();
if (!$apiKey || !Hash::check($rawKey, $apiKey->key_hash)) {
return response()->json(['error' => 'Invalid API key'], 401);
}
$request->setApiKey($apiKey);
return $next($request);
}
}
Rate Limiting
class ApiRateLimiter
{
public function check(ApiKey $apiKey): RateLimitResult
{
$plan = $apiKey->plan;
// Per-minute limit через Redis sliding window
$minuteKey = "rate:{$apiKey->id}:minute:" . floor(time() / 60);
$minuteCount = Redis::incr($minuteKey);
Redis::expire($minuteKey, 120);
if ($minuteCount > $plan->requests_per_minute) {
return RateLimitResult::exceeded(
limit: $plan->requests_per_minute,
reset: (floor(time() / 60) + 1) * 60
);
}
// Monthly limit
$monthKey = "rate:{$apiKey->id}:month:" . date('Y-m');
$monthCount = Redis::incr($monthKey);
Redis::expireat($monthKey, strtotime('first day of next month'));
if ($plan->requests_per_month !== -1 && $monthCount > $plan->requests_per_month) {
return RateLimitResult::quotaExceeded($plan->requests_per_month);
}
return RateLimitResult::ok(
remaining: $plan->requests_per_month === -1
? null
: $plan->requests_per_month - $monthCount
);
}
}
Дашборд использования API
function ApiUsageDashboard({ apiKeyId }: Props) {
const { data } = useQuery({
queryKey: ['api-usage', apiKeyId],
queryFn: () => fetchUsageStats(apiKeyId),
});
return (
<div className="grid grid-cols-3 gap-6">
<StatCard label="Запросов сегодня" value={data?.today} />
<StatCard label="Запросов в месяц" value={data?.month} limit={data?.monthLimit} />
<StatCard label="Среднее время (мс)" value={data?.avgResponseMs} />
</div>
);
}
Сроки
API-монетизация с ключами, тарифами и rate limiting: 8–12 рабочих дней.







