import {z} from 'zod' export type CustomerId = string; export type IdempotencyKey = string; export type AddressApi = {street: string, city: string, state: string, postal_code: string, country: string}; export type CreateVirtualAccountRequest = {developer_fee_percent?: string | undefined, source: {currency?: string | undefined}, metadata?: {[key: string]: unknown} | undefined, workflow_type?: ('fiat_to_crypto' | 'fiat_to_fiat') | undefined, destination?: ({currency: 'usdc' | 'usdt' | 'dai' | 'pyusd' | 'eurc', payment_rail: 'arbitrum' | 'avalanche_c_chain' | 'base' | 'celo' | 'ethereum' | 'optimism' | 'polygon' | 'solana' | 'stellar' | 'tron', address: string, memo?: string | undefined}) | undefined, fiat_destination?: ({type: 'bank', recipient_type?: ('individual' | 'business') | undefined, date_of_birth?: string | undefined, account_holder_name: string, account_number?: string | undefined, iban?: string | undefined, bank_name: string, bank_country: string, currency: string, account_type?: ('savings' | 'checking' | 'payment') | undefined, branch_name?: string | undefined, address?: AddressApi | undefined, swift_code?: string | undefined, bic_code?: string | undefined, ifsc_code?: string | undefined, aba_code?: string | undefined, sort_code?: string | undefined, branch_code?: string | undefined, bsb_code?: string | undefined, bank_code?: string | undefined, cnaps?: string | undefined, name_in_local_language?: string | undefined, national_identification_number?: string | undefined, logistics_tracking_details?: (Array<{tracking_number?: string | undefined, logistics_provider?: ({name?: string | undefined, code?: string | undefined}) | undefined}>) | undefined, documents?: (Array<{type: 'invoice' | 'contract' | 'other', base64_url: string}>) | undefined}) | undefined}; export type CreateApiApiV1CustomersCustomerIdVirtualAccountsArgs = {customerId: CustomerId, 'idempotency-key': IdempotencyKey, body: CreateVirtualAccountRequest}; export type CreateApiApiV1CustomersCustomerIdVirtualAccountsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const addressApiOutput = z.object({street: z.string(), city: z.string(), state: z.string(), postal_code: z.string(), country: z.string()}); export const virtualAccountResponse = z.object({id: z.string(), status: z.enum(["activated", "deactivated", "pending", "closed"]), customer_id: z.string(), developer_fee_percent: z.string().optional(), created_at: z.string(), source_deposit_instructions: z.object({currency: z.string(), payment_rails: z.array(z.enum(["ach_push", "wire", "npp", "sepa", "pix", "spei", "faster_payments"])), bank_name: z.string().optional(), bank_address: z.string().optional(), bank_beneficiary_name: z.string().optional(), bank_beneficiary_address: z.string().optional(), bank_account_number: z.string().optional(), bank_routing_number: z.string().optional(), iban: z.string().optional(), bic: z.string().optional(), pix_key: z.string().optional(), clabe: z.string().optional(), account_holder_name: z.string().optional(), pay_id: z.string().optional(), pay_id_name: z.string().optional()}), deposit_handling_mode: z.enum(["auto_payout", "hold", "manual"]), workflow_type: z.enum(["fiat_to_crypto", "fiat_to_fiat"]), destination: z.object({currency: z.enum(["usdc", "usdt", "dai", "pyusd", "eurc"]), payment_rail: z.enum(["arbitrum", "avalanche_c_chain", "base", "celo", "ethereum", "optimism", "polygon", "solana", "stellar", "tron"]), address: z.string(), memo: z.string().optional()}).nullable(), fiat_destination: z.object({type: z.literal("bank"), recipient_type: z.enum(["individual", "business"]).optional(), date_of_birth: z.string().optional(), account_holder_name: z.string(), account_number: z.string().optional(), iban: z.string().optional(), bank_name: z.string(), bank_country: z.unknown(), currency: z.unknown(), account_type: z.enum(["savings", "checking", "payment"]).optional(), branch_name: z.string().optional(), address: addressApiOutput.optional(), swift_code: z.string().optional(), bic_code: z.string().optional(), ifsc_code: z.string().optional(), aba_code: z.string().optional(), sort_code: z.string().optional(), branch_code: z.string().optional(), bsb_code: z.string().optional(), bank_code: z.string().optional(), cnaps: z.string().optional(), name_in_local_language: z.string().optional(), national_identification_number: z.string().optional(), logistics_tracking_details: z.array(z.object({tracking_number: z.string().optional(), logistics_provider: z.object({name: z.string().optional(), code: z.string().optional()}).optional()})).optional(), documents: z.array(z.object({type: z.enum(["invoice", "contract", "other"]), base64_url: z.string()})).optional()}).nullable(), held_balance: z.object({amount: z.string(), currency: z.string()}).nullable(), deposit_stats: z.object({total_deposit_count: z.number().int().gte(0).lte(9007199254740991), total_deposit_amount: z.string(), last_deposit_at: z.string().nullable()}).optional(), metadata: z.record(z.string(), z.string()).nullable().optional()}); 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 }; /** * Create a virtual account for a customer. deposit_handling_mode is set server-side (defaults to auto_payout). * * POST /api/v1/customers/{customerId}/virtual-accounts * * Also accepts auth: apiKeyAuth */ export const createApiApiV1CustomersCustomerIdVirtualAccounts = async (args: CreateApiApiV1CustomersCustomerIdVirtualAccountsArgs, options?: CreateApiApiV1CustomersCustomerIdVirtualAccountsOptions) => { const {baseUrl = "https://api.stables.money", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['idempotency-key'] != null) headers.set("idempotency-key", String(args['idempotency-key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/customers/${encodeURIComponent(String(args.customerId))}/virtual-accounts${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(virtualAccountResponse, await res.json()) };