Documentation for the Document AI TypeScript SDK
[!NOTE] This package is published with CommonJS and ES Modules (ESM) support.
ReadableStream
Symbol.asyncIterator
tsconfig.json
options are recommended for projects using this
SDK in order to get static type support for features like async iterables,
streams and fetch
-related APIs (for await...of
,
AbortSignal
, Request
, Response
and
so on):
target
can be set to older ECMAScript versions, it may result in extra,
unnecessary compatibility code being generated if you are not targeting old
runtimes.
Name | Type | Scheme | Environment Variable |
---|---|---|---|
apiKeyAuth | http | HTTP Bearer | DOCUMENTAI_API_KEY_AUTH |
apiKeyAuth
parameter must be set when initializing the SDK client instance. For example:
for await...of
syntax.
Here’s an example of one such pagination call:
models/errors/errors.ts
module. The known errors for a method are documented under the Errors tables in SDK docs. For example, the list
method may throw the following errors:
Error Type | Status Code | Content Type |
---|---|---|
errors.BadRequestError | 400 | application/json |
errors.UnauthorizedError | 401 | application/json |
errors.TooManyRequestsError | 429 | application/json |
errors.InternalServerError | 500 | application/json |
errors.APIError | 4XX, 5XX | */* |
APIError
.
SDKValidationError
that is thrown as a result will capture the raw value that failed validation in an attribute called rawValue
. Additionally, a pretty()
method is available on this error that can be used to log a nicely formatted multi-line string since validation errors can list many issues and the plain error string may be difficult read when debugging.
In some rare cases, the SDK can fail to get a response from the server or even make the request due to unexpected circumstances such as network conditions. These types of errors are captured in the models/errors/httpclienterrors.ts
module:
HTTP Client Error | Description |
---|---|
RequestAbortedError | HTTP request was aborted by the client |
RequestTimeoutError | HTTP request timed out due to an AbortSignal signal |
ConnectionError | HTTP client was unable to make a request to a server |
InvalidRequestError | Any input used to create a request is invalid |
UnexpectedClientError | Unrecognised or unexpected error |
serverURL: string
optional parameter when initializing the SDK client instance. For example:
HTTPClient
that wraps the native
Fetch API. This
client is a thin wrapper around fetch
and provides the ability to attach hooks
around the request lifecycle that can be used to modify the request or handle
errors and response.
The HTTPClient
constructor takes an optional fetcher
argument that can be
used to integrate a third-party HTTP client or when writing tests to mock out
the HTTP client and feed in fixtures.
The following example shows how to use the "beforeRequest"
hook to to add a
custom header and a timeout to requests and how to use the "requestError"
hook
to log errors:
console
’s interface as an SDK option.
[!WARNING] Beware that debug logging will reveal secrets, like API tokens in headers, in log messages printed to a console or files. It’s recommended to use this feature only during local development and not in production.
DOCUMENTAI_DEBUG
to true.