// This file was auto-generated by Fern from our API Definition. import type { BaseClientOptions, BaseRequestOptions } from "../../../../BaseClient.js"; import { type NormalizedClientOptionsWithAuth, normalizeClientOptionsWithAuth } from "../../../../BaseClient.js"; import { mergeHeaders, mergeOnlyDefinedHeaders } from "../../../../core/headers.js"; import * as core from "../../../../core/index.js"; import { mergeAdditionalBodyParameters } from "../../../../core/requestBody.js"; import * as environments from "../../../../environments.js"; import { handleNonStatusCodeError } from "../../../../errors/handleNonStatusCodeError.js"; import * as errors from "../../../../errors/index.js"; import * as Plaid from "../../../index.js"; export declare namespace InstitutionsClient { export type Options = BaseClientOptions; export interface RequestOptions extends BaseRequestOptions {} } export class InstitutionsClient { protected readonly _options: NormalizedClientOptionsWithAuth; constructor(options: InstitutionsClient.Options = {}) { this._options = normalizeClientOptionsWithAuth(options); } /** * Returns a JSON response containing details on all financial institutions currently supported by Plaid. Because Plaid supports thousands of institutions, results are paginated. * * If there is no overlap between an institution's enabled products and a client's enabled products, then the institution will be filtered out from the response. As a result, the number of institutions returned may not match the count specified in the call. * * @param {Plaid.InstitutionsGetRequest} request * @param {InstitutionsClient.RequestOptions} requestOptions - Request-specific configuration. * * @throws {@link Plaid.BadRequestError} * @throws {@link Plaid.UnauthorizedError} * @throws {@link Plaid.TooManyRequestsError} * @throws {@link Plaid.InternalServerError} * @throws {@link Plaid.ClientRequestError} * @throws {@link Plaid.ServerError} * @throws {@link errors.PlaidError} * @throws {@link errors.PlaidTimeoutError} * * @example * await client.institutions.get({ * count: 1, * offset: 1, * country_codes: ["US"] * }) */ public async get( request: Plaid.InstitutionsGetRequest, requestOptions?: InstitutionsClient.RequestOptions, ): Promise> { const list = core.HttpResponsePromise.interceptFunction( async ( request: Plaid.InstitutionsGetRequest, ): Promise> => { const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest(); const _headers: core.Fetcher.Args["headers"] = mergeHeaders( _authRequest.headers, this._options?.headers, mergeOnlyDefinedHeaders({ "PLAID-CLIENT-ID": requestOptions?.clientId ?? this._options?.clientId ?? (typeof process !== "undefined" ? process.env?.PLAID_CLIENT_ID : undefined), "Plaid-Version": requestOptions?.version ?? this._options?.version ?? (typeof process !== "undefined" ? process.env?.PLAID_VERSION : undefined) ?? "2020-09-14", }), requestOptions?.headers, ); const _response = await core.fetcher({ url: core.url.join( (await core.Supplier.get(this._options.baseUrl)) ?? (await core.Supplier.get(this._options.environment)) ?? environments.PlaidEnvironment.Production, "institutions/get", ), method: "POST", headers: _headers, contentType: "application/json", queryString: core.url.queryBuilder().mergeAdditional(requestOptions?.queryParams).build(), requestType: "json", body: mergeAdditionalBodyParameters(request, requestOptions?.additionalBodyParameters), timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : this._options?.timeoutInSeconds != null ? this._options?.timeoutInSeconds * 1000 : undefined, maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries, abortSignal: requestOptions?.abortSignal, fetchFn: this._options?.fetch, logging: this._options.logging, }); if (_response.ok) { return { data: _response.body as Plaid.InstitutionsGetResponse, rawResponse: _response.rawResponse, }; } if (_response.error.reason === "status-code") { switch (_response.error.statusCode) { case 400: throw new Plaid.BadRequestError( _response.error.body as Plaid.PlaidError | null, _response.rawResponse, ); case 401: throw new Plaid.UnauthorizedError( _response.error.body as Plaid.PlaidError | null, _response.rawResponse, ); case 429: throw new Plaid.TooManyRequestsError( _response.error.body as Plaid.PlaidError | null, _response.rawResponse, ); case 500: throw new Plaid.InternalServerError( _response.error.body as Plaid.PlaidError | null, _response.rawResponse, ); default: throw new errors.PlaidError({ statusCode: _response.error.statusCode, body: _response.error.body, rawResponse: _response.rawResponse, }); } } return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/institutions/get"); }, ); let _offset = request?.offset != null ? request?.offset : 1; const dataWithRawResponse = await list(request).withRawResponse(); return new core.Page({ response: dataWithRawResponse.data, rawResponse: dataWithRawResponse.rawResponse, hasNextPage: (response) => (response?.institutions ?? []).length > 0 && (request?.count == null || (response?.institutions ?? []).length >= request?.count), getItems: (response) => response?.institutions ?? [], loadPage: (response) => { const _nextOffset = _offset + (response?.institutions != null ? response.institutions.length : 1); return core.HttpResponsePromise.fromPromise( list(core.setObjectProperty(request, "offset", _nextOffset)) .withRawResponse() .then((_pageResponse) => { _offset = _nextOffset; return _pageResponse; }), ); }, }); } /** * Returns a JSON response containing details for institutions that match the query parameters, up to a maximum of ten institutions per query. Institutions that Plaid does not support for new connections are not included in search results, but can still be retrieved by ID with `/institutions/get_by_id`. * * Versioning note: API versions 2019-05-29 and earlier allow use of the `public_key` parameter instead of the `client_id` and `secret` parameters to authenticate to this endpoint. The `public_key` parameter has since been deprecated; all customers are encouraged to use `client_id` and `secret` instead. * * @param {Plaid.InstitutionsSearchRequest} request * @param {InstitutionsClient.RequestOptions} requestOptions - Request-specific configuration. * * @throws {@link Plaid.BadRequestError} * @throws {@link Plaid.UnauthorizedError} * @throws {@link Plaid.TooManyRequestsError} * @throws {@link Plaid.InternalServerError} * @throws {@link Plaid.ClientRequestError} * @throws {@link Plaid.ServerError} * @throws {@link errors.PlaidError} * @throws {@link errors.PlaidTimeoutError} * * @example * await client.institutions.search({ * query: "query", * country_codes: ["US"] * }) */ public search( request: Plaid.InstitutionsSearchRequest, requestOptions?: InstitutionsClient.RequestOptions, ): core.HttpResponsePromise { return core.HttpResponsePromise.fromPromise(this.__search(request, requestOptions)); } private async __search( request: Plaid.InstitutionsSearchRequest, requestOptions?: InstitutionsClient.RequestOptions, ): Promise> { const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest(); const _headers: core.Fetcher.Args["headers"] = mergeHeaders( _authRequest.headers, this._options?.headers, mergeOnlyDefinedHeaders({ "PLAID-CLIENT-ID": requestOptions?.clientId ?? this._options?.clientId ?? (typeof process !== "undefined" ? process.env?.PLAID_CLIENT_ID : undefined), "Plaid-Version": requestOptions?.version ?? this._options?.version ?? (typeof process !== "undefined" ? process.env?.PLAID_VERSION : undefined) ?? "2020-09-14", }), requestOptions?.headers, ); const _response = await core.fetcher({ url: core.url.join( (await core.Supplier.get(this._options.baseUrl)) ?? (await core.Supplier.get(this._options.environment)) ?? environments.PlaidEnvironment.Production, "institutions/search", ), method: "POST", headers: _headers, contentType: "application/json", queryString: core.url.queryBuilder().mergeAdditional(requestOptions?.queryParams).build(), requestType: "json", body: mergeAdditionalBodyParameters(request, requestOptions?.additionalBodyParameters), timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : this._options?.timeoutInSeconds != null ? this._options?.timeoutInSeconds * 1000 : undefined, maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries, abortSignal: requestOptions?.abortSignal, fetchFn: this._options?.fetch, logging: this._options.logging, }); if (_response.ok) { return { data: _response.body as Plaid.InstitutionsSearchResponse, rawResponse: _response.rawResponse }; } if (_response.error.reason === "status-code") { switch (_response.error.statusCode) { case 400: throw new Plaid.BadRequestError( _response.error.body as Plaid.PlaidError | null, _response.rawResponse, ); case 401: throw new Plaid.UnauthorizedError( _response.error.body as Plaid.PlaidError | null, _response.rawResponse, ); case 429: throw new Plaid.TooManyRequestsError( _response.error.body as Plaid.PlaidError | null, _response.rawResponse, ); case 500: throw new Plaid.InternalServerError( _response.error.body as Plaid.PlaidError | null, _response.rawResponse, ); default: throw new errors.PlaidError({ statusCode: _response.error.statusCode, body: _response.error.body, rawResponse: _response.rawResponse, }); } } return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/institutions/search"); } /** * Returns a JSON response containing details on a specified financial institution. * * Institutions that Plaid does not support for new connections are still returned by this endpoint, so that an `institution_id` stored on an existing Item can still be looked up. These institutions are not returned by `/institutions/get` or `/institutions/search`, and attempting to create a new Item for one of them returns an `INSTITUTION_NO_LONGER_SUPPORTED` error. Use `connection_availability` to tell the two cases apart. * * Versioning note: API versions 2019-05-29 and earlier allow use of the `public_key` parameter instead of the `client_id` and `secret` to authenticate to this endpoint. The `public_key` has been deprecated; all customers are encouraged to use `client_id` and `secret` instead. * * @param {Plaid.InstitutionsGetByIdRequest} request * @param {InstitutionsClient.RequestOptions} requestOptions - Request-specific configuration. * * @throws {@link Plaid.BadRequestError} * @throws {@link Plaid.UnauthorizedError} * @throws {@link Plaid.TooManyRequestsError} * @throws {@link Plaid.InternalServerError} * @throws {@link Plaid.ClientRequestError} * @throws {@link Plaid.ServerError} * @throws {@link errors.PlaidError} * @throws {@link errors.PlaidTimeoutError} * * @example * await client.institutions.getById({ * institution_id: "institution_id", * country_codes: ["US"] * }) */ public getById( request: Plaid.InstitutionsGetByIdRequest, requestOptions?: InstitutionsClient.RequestOptions, ): core.HttpResponsePromise { return core.HttpResponsePromise.fromPromise(this.__getById(request, requestOptions)); } private async __getById( request: Plaid.InstitutionsGetByIdRequest, requestOptions?: InstitutionsClient.RequestOptions, ): Promise> { const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest(); const _headers: core.Fetcher.Args["headers"] = mergeHeaders( _authRequest.headers, this._options?.headers, mergeOnlyDefinedHeaders({ "PLAID-CLIENT-ID": requestOptions?.clientId ?? this._options?.clientId ?? (typeof process !== "undefined" ? process.env?.PLAID_CLIENT_ID : undefined), "Plaid-Version": requestOptions?.version ?? this._options?.version ?? (typeof process !== "undefined" ? process.env?.PLAID_VERSION : undefined) ?? "2020-09-14", }), requestOptions?.headers, ); const _response = await core.fetcher({ url: core.url.join( (await core.Supplier.get(this._options.baseUrl)) ?? (await core.Supplier.get(this._options.environment)) ?? environments.PlaidEnvironment.Production, "institutions/get_by_id", ), method: "POST", headers: _headers, contentType: "application/json", queryString: core.url.queryBuilder().mergeAdditional(requestOptions?.queryParams).build(), requestType: "json", body: mergeAdditionalBodyParameters(request, requestOptions?.additionalBodyParameters), timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : this._options?.timeoutInSeconds != null ? this._options?.timeoutInSeconds * 1000 : undefined, maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries, abortSignal: requestOptions?.abortSignal, fetchFn: this._options?.fetch, logging: this._options.logging, }); if (_response.ok) { return { data: _response.body as Plaid.InstitutionsGetByIdResponse, rawResponse: _response.rawResponse }; } if (_response.error.reason === "status-code") { switch (_response.error.statusCode) { case 400: throw new Plaid.BadRequestError( _response.error.body as Plaid.PlaidError | null, _response.rawResponse, ); case 401: throw new Plaid.UnauthorizedError( _response.error.body as Plaid.PlaidError | null, _response.rawResponse, ); case 429: throw new Plaid.TooManyRequestsError( _response.error.body as Plaid.PlaidError | null, _response.rawResponse, ); case 500: throw new Plaid.InternalServerError( _response.error.body as Plaid.PlaidError | null, _response.rawResponse, ); default: throw new errors.PlaidError({ statusCode: _response.error.statusCode, body: _response.error.body, rawResponse: _response.rawResponse, }); } } return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/institutions/get_by_id"); } }