能不能增加一个余额显示?
yzq313 opened this issue · comments
前台能不能增加一个账户余额显示?
你指的是让用户自行输入key的时候显示余额?
你指的是让用户自行输入key的时候显示余额?
对的
余额调用方式如下
$ curl -s "https://api.openai.com/dashboard/billing/credit_grants" \
--header 'Content-type: application/json' \
--header 'Authorization: Bearer api_key' | jq # 这里替换api_key
{
"object": "credit_summary",
"total_granted": 18,
"total_used": 0.178104, # 已用
"total_available": 17.821896, # 剩余可用余额
"grants": {
"object": "list",
"data": [
{
"object": "credit_grant",
"id": "be32c280-ad4a-4e14-bc1a-077a32275f9e",
"grant_amount": 18,
"used_amount": 0.178104,
"effective_at": 1677628800,
"expires_at": 1688169600
}
]
}
}
已经有热心网友给了代码,有需要的话自己加上吧
We are testing this in the classroom. Simple example below but does not always show the available balance:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/dashboard/billing/credit_grants');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer sk-YOUR-KEY-HERE';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$data = json_decode($result,true);
echo "<table>";
foreach ($data as $key => $value) {
echo "<tr>";
echo "<td>" . $key . "</td>";
echo "<td>" . $value . "</td>";
echo "</tr>";
}
echo "</table>";
?>
We are testing this in the classroom. Simple example below but does not always show the available balance:
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/dashboard/billing/credit_grants'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); $headers = array(); $headers[] = 'Content-Type: application/json'; $headers[] = 'Authorization: Bearer sk-YOUR-KEY-HERE'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch); $data = json_decode($result,true); echo "<table>"; foreach ($data as $key => $value) { echo "<tr>"; echo "<td>" . $key . "</td>"; echo "<td>" . $value . "</td>"; echo "</tr>"; } echo "</table>"; ?>
Did you try curl command? Did it work?
@baby9 Unfortunately we can only use PHP CURL in the classroom, so cannot try in commandline.
In case anyone following, this info may be of use:
<?php
// Set the start date (e.g., 1-30 days ago)
$start_date = date('Y-m-d', strtotime('-1 days')); //one day
// Set the end date (e.g., today)
$end_date = date('Y-m-d');
$ch = curl_init();
//curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/dashboard/billing/usage');
// Append the start_date and end_date parameters to the URL
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/dashboard/billing/usage?start_date=' . $start_date . '&end_date=' . $end_date);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer sk-YOUR-KEY-HERE';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
// Print out the raw response body
echo $result;
}
curl_close($ch);
// Decode the JSON response
$response = json_decode($result);
// Access and print the available credits
echo "Available credits: " . $response->available_credits;
?>
Returns lots of information.