Hello Yuri Uiri,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I understand that you like to extract specific rate-limiting headers (x-ratelimit-remaining-requests
, x-ratelimit-remaining-tokens
, and x-request-time
) from the OpenAI API response using Axios or Curl.
If you would like to use Axios:
const axios = require('axios');
async function fetchHeaders() {
try {
const response = await axios.get('https://api.openai.com/v1/engines/davinci/completions', {
headers: {
'Authorization': `Bearer YOUR_API_KEY`, // Replace with your OpenAI API key
},
});
const remainingRequests = response.headers['x-ratelimit-remaining-requests'];
const remainingTokens = response.headers['x-ratelimit-remaining-tokens'];
const requestTime = response.headers['x-request-time'];
console.log('Remaining Requests:', remainingRequests || 'N/A');
console.log('Remaining Tokens:', remainingTokens || 'N/A');
console.log('Request Time:', requestTime || 'N/A');
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
}
fetchHeaders();
Replace YOUR_API_KEY
with a valid API key and handle cases where headers might be absent by providing default values (N/A
).
If you would like to use Curl:
curl -i https://api.openai.com/v1/engines/davinci/completions \
-H "Authorization: Bearer YOUR_API_KEY"
To extract specific headers:
curl -i https://api.openai.com/v1/engines/davinci/completions \
-H "Authorization: Bearer YOUR_API_KEY" | grep -i 'x-ratelimit-remaining-requests\|x-ratelimit-remaining-tokens\|x-request-time'
The -H option sets the required authorization header, while grep filters the headers of interest.
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.