Edits history of script submission #9054 for ' Create Record (datocms)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Datocms = {
    	apiKey: string
    }
    
    export async function main(resource: Datocms, modelId: string, recordData: Record<string, any>) {
    	const endpoint = `https://site-api.datocms.com/items`
    
    	const body = {
    		data: {
    			type: 'item',
    			attributes: recordData,
    			relationships: {
    				item_type: {
    					data: {
    						type: 'item_type',
    						id: modelId
    					}
    				}
    			}
    		}
    	}
    
    	const response = await fetch(endpoint, {
    		method: 'POST',
    		headers: {
    			Authorization: `Bearer ${resource.apiKey}`,
    			Accept: 'application/json',
    			'X-Api-Version': '3',
    			'Content-Type': 'application/vnd.api+json'
    		},
    		body: JSON.stringify(body)
    	})
    
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const data = await response.json()
    
    	return data
    }
    

    Submitted by hugo697 621 days ago