Edits history of script submission #9364 for ' Create a new account (ynab)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Ynab = {
    	token: string
    }
    
    export async function main(
    	auth: Ynab,
    	budget_id: string,
    	body: {
    		account: {
    			name: string
    			type:
    				| 'checking'
    				| 'savings'
    				| 'cash'
    				| 'creditCard'
    				| 'lineOfCredit'
    				| 'otherAsset'
    				| 'otherLiability'
    				| 'mortgage'
    				| 'autoLoan'
    				| 'studentLoan'
    				| 'personalLoan'
    				| 'medicalDebt'
    				| 'otherDebt'
    			balance: number
    		}
    	}
    ) {
    	const url = new URL(`https://api.ynab.com/v1/budgets/${budget_id}/accounts`)
    
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization: 'Bearer ' + auth.token
    		},
    		body: JSON.stringify(body)
    	})
    
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    
    	return await response.json()
    }
    

    Submitted by hugo697 581 days ago