Uploading File To Model Using Write API

What are you trying to accomplish
I want to upload images when creating new model using the Write API

Code stack you are integrating Builder with

Is it possible to upload an image directly to model using the Write API? I am not seeing any documentation to see if this is possible.

Thank you.

Hello @doom_slayer,

You can certainly use Write API to upload images

Here is an example

const fetch = require("node-fetch");
var myHeaders = new fetch.Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Authorization", "Bearer bpk-Your-private-key");

var raw = JSON.stringify({
  "ownerId": "space-id",
  "@version": 3,
  "name": "Facebook Inc",
  "modelId": "model-id",
  "published": "draft",
  "query": [],
  "data": {
    "name": "Facebook",
    "displayName": "fa",
    "logo": "https://cdn.builder.io/api/v1/image/assets%2Fc782aff3c66f48acb425981b997feb10%2F9f2120fe81ff444084dc2819d73f07fb"

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'

fetch("https://builder.io/api/v1/write/companies", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

If you are looking to upload default images for file fields while creating a model then you might need to use Admin API.

You may also check Upload API - Builder.io

Hope this helps!

Ohh okay. So I would upload the file first using the Upload API, then assign the file url to the model?

Hello @doom_slayer,

That’s right, you can also use an external image URL to upload the images.