Path: blob/master/src/packages/next/pages/api/v2/purchases/is-purchase-allowed.ts
1454 views
/*1Determine whether or not user can purchase some amount of a given2service via pay-as-you-go, given all quotas and current balance.34PARAMS:56- service (required): one of the services, e.g., openai-gpt-47- cost (optional): cost in dollars of that service; if not given, some amount may be8chosen based on the service, e.g., for gpt-4 it is the maximum cost of a single API call.910RETURNS:1112- {allowed:boolean; discouraged?:boolean; reason?:string} or {error:message} (e.g., if not signed in)13*/1415import getAccountId from "lib/account/get-account";16import { isPurchaseAllowed } from "@cocalc/server/purchases/is-purchase-allowed";17import getParams from "lib/api/get-params";1819export default async function handle(req, res) {20try {21res.json(await get(req));22} catch (err) {23res.json({ error: `${err.message}` });24return;25}26}2728async function get(req) {29const account_id = await getAccountId(req);30if (account_id == null) {31throw Error("must be signed in");32}33const { service, cost } = getParams(req);34return await isPurchaseAllowed({ account_id, service, cost });35}363738