Node.js
This library provides convenient access to the Raccoon AI REST API from server-side TypeScript or JavaScript.
Installation
Usage
While you can provide a secretKey
keyword argument,
we recommend using environment variables for maximum security.
to add RACCOON_SECRET_KEY="My Secret Key"
to your .env
file
so that your Secret Key is not stored in source control.
Streaming responses
We provide support for streaming responses using Server Sent Events (SSE).
If you need to cancel a stream, you can break
from the loop
or call stream.controller.abort()
.
Retries
Certain errors will be automatically retried 0 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors will all be retried by default.
You can use the maxRetries
option to configure or disable this:
Timeouts
Requests time out after 10 minutes by default. You can configure this with a timeout
option:
On timeout, an APIConnectionTimeoutError
is thrown.
Note that requests which time out will be retried twice by default.
Advanced
Customizing the fetch client
By default, this library uses node-fetch
in Node, and expects a global fetch
function in other environments.
If you would prefer to use a global, web-standards-compliant fetch
function even in a Node environment,
(for example, if you are running Node with --experimental-fetch
or using NextJS which polyfills with undici
),
add the following import before your first import from "RaccoonAI"
:
To do the inverse, add import "raccoonai/shims/node"
(which does import polyfills).
This can also be useful if you are getting the wrong TypeScript types for Response
(more details).
Logging and middleware
You may also provide a custom fetch
function when instantiating the client,
which can be used to inspect or alter the Request
or Response
before/after each request:
Note that if given a DEBUG=true
environment variable, this library will log all requests and responses automatically.
This is intended for debugging purposes only and may change in the future without notice.
Requirements
TypeScript >= 4.5 is supported.
The following runtimes are supported:
- Web browsers (Up-to-date Chrome, Firefox, Safari, Edge, and more)
- Node.js 18 LTS or later (non-EOL) versions.
- Deno v1.28.0 or higher.
- Bun 1.0 or later.
- Cloudflare Workers.
- Vercel Edge Runtime.
- Jest 28 or greater with the
"node"
environment ("jsdom"
is not supported at this time). - Nitro v2.6 or greater.
Note that React Native is not supported at this time.
Was this page helpful?