import {z} from 'zod' export type BaselineKycCheckDataV0 = {country_code: string, last_name: string, address_country_code: string, first_name?: (string | null) | undefined, middle_name?: (string | null) | undefined, email?: (string | null) | undefined, tax_id_number?: (string | null) | undefined, birth_date?: (string | null) | undefined, address_street_1?: (string | null) | undefined, address_street_2?: (string | null) | undefined, address_city?: (string | null) | undefined, address_subdivision?: (string | null) | undefined, address_postal_code?: (string | null) | undefined}; export type CreateBaselineKycInput = {data: BaselineKycCheckDataV0, provider_name: string, jurisdiction?: 'BASELINE' | undefined}; export type UsKycIdentityDataV1 = {given_name: string, family_name: string, email_address: string, phone_number: string, street_address: string, city: string, country_of_tax_residence: 'US', postal_code: string, date_of_birth: string, tax_id: string, country_of_citizenship: string, middle_name?: (string | null) | undefined, unit?: (string | null) | undefined, state?: (string | null) | undefined}; export type FundingSource = 'EMPLOYMENT_INCOME' | 'INVESTMENTS' | 'INHERITANCE' | 'BUSINESS_INCOME' | 'SAVINGS' | 'FAMILY'; export type UsEntityKycFinancialProfileInfoV1 = {funding_sources: Array, liquid_net_worth_min: number, liquid_net_worth_max: number}; export type EmploymentStatus = 'UNEMPLOYED' | 'EMPLOYED' | 'STUDENT' | 'RETIRED'; export type UsEntityKycEmploymentInfoV1 = {employment_status: EmploymentStatus, employer_name?: (string | null) | undefined, employer_address?: (string | null) | undefined, employment_position?: (string | null) | undefined}; export type UsEntityKycRiskDisclosureInfoV1 = {is_control_person: boolean, is_affiliated_exchange_or_finra: boolean, is_politically_exposed: boolean, immediate_family_exposed: boolean}; export type UsEntityTrustedContactInfoV1 = {given_name: string, family_name: string, email_address?: (string | null) | undefined, phone_number?: (string | null) | undefined}; export type AlpacaCustomerAgreementInfoV1 = {signed_at: string, ip_address: unknown}; export type UsEntityKycMetadataV1 = {ref_id: string, check_initiated_at: string, check_completed_at: string, ip_address: unknown}; export type UsEntityKycCitationInfoV1 = {address_sources: Array, date_of_birth_sources: Array, tax_id_sources: Array}; export type UsEntityAmlCheckInfoV1 = {ref_id: string, check_created_at: string, is_politically_exposed_person_match: boolean, is_adverse_media_match: boolean, is_sanctions_match: boolean, is_monitored_lists_match: boolean, records: Array}; export type UsNonProfessionalTraderAttestationV1 = {is_non_professional_trader: boolean, attestation_dt: string}; export type UsVisaType = 'B1' | 'B2' | 'DACA' | 'E1' | 'E2' | 'E3' | 'F1' | 'G4' | 'H1B' | 'J1' | 'L1' | 'Other' | 'O1' | 'TN1'; export type UsEntityKycUsImmigrationInfoV1 = {is_permanent_resident: boolean, country_of_birth: string, visa_type?: UsVisaType | undefined, visa_expiration_date?: (string | null) | undefined, departure_from_us_date?: (string | null) | undefined}; export type UsKycCheckDataV1 = {identity: UsKycIdentityDataV1, financial_profile: UsEntityKycFinancialProfileInfoV1, employment: UsEntityKycEmploymentInfoV1, risk_disclosure: UsEntityKycRiskDisclosureInfoV1, trusted_contact: UsEntityTrustedContactInfoV1, alpaca_customer_agreement: AlpacaCustomerAgreementInfoV1, kyc_metadata: UsEntityKycMetadataV1, data_citation: UsEntityKycCitationInfoV1, aml_check: UsEntityAmlCheckInfoV1, non_professional_trader_attestation: UsNonProfessionalTraderAttestationV1, us_immigration_info?: (UsEntityKycUsImmigrationInfoV1 | null) | undefined}; export type CreateUsKycInput = {data: UsKycCheckDataV1, provider_name: string, jurisdiction?: 'US' | undefined}; export type CreateKycInput = CreateBaselineKycInput | CreateUsKycInput; export type CreateApiApiV2EntitiesEntityIdKycArgs = {body: CreateKycInput}; export type CreateApiApiV2EntitiesEntityIdKycOptions = {baseUrl?: string | undefined, ApiKeyId?: string | undefined, ApiSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const kycStatus = z.enum(["PASS", "FAIL", "PENDING", "INCOMPLETE", "NEEDS_REVIEW"]); export const baselineKycCheckDataV0 = z.object({country_code: z.string().min(2).max(2), last_name: z.string().min(1), address_country_code: z.string().min(2).max(2), first_name: z.string().min(1).nullable().optional(), middle_name: z.string().min(1).nullable().optional(), email: z.string().nullable().optional(), tax_id_number: z.string().min(1).nullable().optional(), birth_date: z.string().nullable().optional(), address_street_1: z.string().min(1).nullable().optional(), address_street_2: z.string().nullable().optional(), address_city: z.string().min(1).nullable().optional(), address_subdivision: z.string().min(1).nullable().optional(), address_postal_code: z.string().min(1).nullable().optional()}); export const baselineKyc = z.object({id: z.string(), status: kycStatus, checked_dt: z.string().nullable().optional(), data: baselineKycCheckDataV0.nullable().optional(), jurisdiction: z.literal("BASELINE").optional()}); export const usKycIdentityDataV1 = z.object({given_name: z.string().min(1), family_name: z.string().min(2), email_address: z.string(), phone_number: z.string(), street_address: z.string().min(2), city: z.string().min(2), country_of_tax_residence: z.literal("US"), postal_code: z.string().min(1), date_of_birth: z.string(), tax_id: z.string(), country_of_citizenship: z.string().min(2).max(2), middle_name: z.string().min(1).nullable().optional(), unit: z.string().min(1).nullable().optional(), state: z.string().min(1).nullable().optional()}); export const fundingSource = z.enum(["EMPLOYMENT_INCOME", "INVESTMENTS", "INHERITANCE", "BUSINESS_INCOME", "SAVINGS", "FAMILY"]); export const usEntityKycFinancialProfileInfoV1 = z.object({funding_sources: z.array(fundingSource), liquid_net_worth_min: z.number().int().gte(0), liquid_net_worth_max: z.number().int().gte(0)}); export const employmentStatus = z.enum(["UNEMPLOYED", "EMPLOYED", "STUDENT", "RETIRED"]); export const usEntityKycEmploymentInfoV1 = z.object({employment_status: employmentStatus, employer_name: z.string().min(1).nullable().optional(), employer_address: z.string().min(1).nullable().optional(), employment_position: z.string().min(1).nullable().optional()}); export const usEntityKycRiskDisclosureInfoV1 = z.object({is_control_person: z.boolean(), is_affiliated_exchange_or_finra: z.boolean(), is_politically_exposed: z.boolean(), immediate_family_exposed: z.boolean()}); export const usEntityTrustedContactInfoV1 = z.object({given_name: z.string(), family_name: z.string(), email_address: z.string().nullable().optional(), phone_number: z.string().nullable().optional()}); export const alpacaCustomerAgreementInfoV1 = z.object({signed_at: z.string(), ip_address: z.string()}); export const usEntityKycMetadataV1 = z.object({ref_id: z.string().min(1), check_initiated_at: z.string(), check_completed_at: z.string(), ip_address: z.string()}); export const usEntityKycCitationInfoV1 = z.object({address_sources: z.array(z.string()), date_of_birth_sources: z.array(z.string()), tax_id_sources: z.array(z.string())}); export const usEntityAmlCheckInfoV1 = z.object({ref_id: z.string().min(1), check_created_at: z.string(), is_politically_exposed_person_match: z.boolean(), is_adverse_media_match: z.boolean(), is_sanctions_match: z.boolean(), is_monitored_lists_match: z.boolean(), records: z.array(z.string())}); export const usNonProfessionalTraderAttestationV1 = z.object({is_non_professional_trader: z.boolean(), attestation_dt: z.string()}); export const usVisaType = z.enum(["B1", "B2", "DACA", "E1", "E2", "E3", "F1", "G4", "H1B", "J1", "L1", "Other", "O1", "TN1"]); export const usEntityKycUsImmigrationInfoV1 = z.object({is_permanent_resident: z.boolean(), country_of_birth: z.string().min(2).max(2), visa_type: usVisaType.optional(), visa_expiration_date: z.string().nullable().optional(), departure_from_us_date: z.string().nullable().optional()}); export const usKycCheckDataV1 = z.object({identity: usKycIdentityDataV1, financial_profile: usEntityKycFinancialProfileInfoV1, employment: usEntityKycEmploymentInfoV1, risk_disclosure: usEntityKycRiskDisclosureInfoV1, trusted_contact: usEntityTrustedContactInfoV1, alpaca_customer_agreement: alpacaCustomerAgreementInfoV1, kyc_metadata: usEntityKycMetadataV1, data_citation: usEntityKycCitationInfoV1, aml_check: usEntityAmlCheckInfoV1, non_professional_trader_attestation: usNonProfessionalTraderAttestationV1, us_immigration_info: usEntityKycUsImmigrationInfoV1.nullable().optional()}); export const usKyc = z.object({id: z.string(), status: kycStatus, checked_dt: z.string().nullable().optional(), data: usKycCheckDataV1.nullable().optional(), jurisdiction: z.literal("US").optional()}); export const kyc = z.discriminatedUnion("jurisdiction", [baselineKyc, usKyc]); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Submit KYC Data * * POST /api/v2/entities/{entity_id}/kyc */ export const createApiApiV2EntitiesEntityIdKyc = async (args: CreateApiApiV2EntitiesEntityIdKycArgs, options?: CreateApiApiV2EntitiesEntityIdKycOptions) => { const {baseUrl = "https://api-enterprise.sbt.dinari.com", fetch: fetchFn = fetch, ApiKeyId, ApiSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyId !== undefined) headers.set("X-API-Key-Id", ApiKeyId) if (ApiSecretKey !== undefined) headers.set("X-API-Secret-Key", ApiSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/entities/${encodeURIComponent(String(args.entity_id))}/kyc${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(kyc, await res.json()) };