ChainGPT AI News SDK
This library provides convenient access to the ChainGPT AI News REST API from TypeScript or JavaScript.
Installation
npm install --save @chaingpt/ainews
yarn add ainews
Usage
import { AINews } from '@chaingpt/ainews';
const ainews = new AINews({
apiKey: 'Your ChainGPT API Key',
});
async function main() {
const news = await ainews.getNews({});
}
main();
Handling errors
When the library is unable to connect to the API,
or if the API returns a non-success status code (i.e., 4xx or 5xx response),
a subclass of AINewsError
will be thrown:
import { Errors } from '@chaingpt/ainews';
async function main() {
ainews.getNews({}).then((res)=>{}).catch((err)=>{
if (err instanceof Errors.AINewsError) {
if (err instanceof Errors.InvalidApiKeyError) {
console.log("Invalid Api Key Error")
} else if (err instanceof Errors.RateLimitExceededError) {
console.log("Rate limit exceeded Error")
}
}
});
}
main();
Error codes are as followed:
Status Code | Error Type |
---|
403 | InvalidApiKeyError |
429 | RateLimitExceededError |
N/A | AINewsError |