Typescript SDK
Learn how to use Supermemory with Typescript
Installation
Basic Usage
To use the SDK, you’ll need an API key from Supermemory. Learn how to get one from Getting Started.
Type Safety
This library includes TypeScript definitions for all request parameters and response fields, providing excellent type safety and autocompletion in modern editors.
Error Handling
When the library is unable to connect to the API or if the API returns a non-success status code (4xx or 5xx), a subclass of APIError
will be thrown.
Error Types
The SDK provides specific error types for different error scenarios:
BadRequestError
(400)AuthenticationError
(401)PermissionDeniedError
(403)NotFoundError
(404)UnprocessableEntityError
(422)RateLimitError
(429)InternalServerError
(500+)APIConnectionError
(network issues)APIConnectionTimeoutError
(request timeout)
Retries and Timeouts
Automatic Retries
Certain errors are automatically retried 2 times by default, with a short exponential backoff. Connection errors, 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors will all be retried by default.
Configuring Timeouts
Requests time out after 1 minute 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 Usage
Accessing Raw Response Data
The “raw” Response returned by fetch()
can be accessed through the .asResponse()
method on the APIPromise type that all methods return.
You can also use the .withResponse()
method to get the raw Response along with the parsed data:
Logging
Important: All log messages are intended for debugging only. The format and content of log messages may change between releases.
Log Levels
The log level can be configured in two ways:
- Via the
SUPERMEMORY_LOG
environment variable - Using the
logLevel
client option (overrides the environment variable if set)
Available log levels, from most to least verbose:
'debug'
- Show debug messages, info, warnings, and errors'info'
- Show info messages, warnings, and errors'warn'
- Show warnings and errors (default)'error'
- Show only errors'off'
- Disable all logging
At the ‘debug’ level, all HTTP requests and responses are logged, including headers and bodies. Some authentication-related headers are redacted, but sensitive data in request and response bodies may still be visible.
Custom Logger
By default, this library logs to globalThis.console
. You can also provide a custom logger:
Making Custom/Undocumented Requests
To make requests to undocumented endpoints, you can use client.get
, client.post
, and other HTTP verbs:
Customizing the Fetch Client
By default, this library expects a global fetch
function is defined. If you want to use a different fetch function, you can pass it to the client:
Fetch Options
If you want to set custom fetch options without overriding the fetch function, you can provide a fetchOptions
object when instantiating the client or making a request:
Configuring Proxies
API Reference
For a complete API reference, please see the API documentation on GitHub.