Post request to custom model is not working

What are you trying to accomplish
I want to post data to a custom model. I have tried write api but its not working and giving 404 error. Please let me know how can i achieve it?

Code stack you are integrating Builder with
NextJS, swell js, typescript

Reproducible code example

const dataObject = {
   name: 'Test',
    data: {
      userName: 'Anu',
      review: 'Testing',
      productSlug: 'demo-product',
      date: new Date(),
      rating: 4
    }
  }
  const postData = async (url: string, data: any) => {
    const response = await fetch(url, {
      body: JSON.stringify(data),
      headers: {
        Authorization: "Bearer " + process.env.privateKey,
        "Content-Type": "application/json"
      },
      method: "POST"
    })

    return response.json();
  }
  postData('https://builder.io/api/v1/write/reviews', dataObject)
    .then((data) => {
      console.log('data: ', data);
      
    }) 

hey @anupama.sharmaa that looks correct. I was able to copy the code and test directly in my local and successfully create a review content entry.

If you are having issues, the main things to confirm are:

  1. Make sure the reviews model exists on your space
  2. Make sure the private key is correct for that space (it is different from the API Key and starts with ā€˜bpk-ā€™)
  3. Make sure the model has all the correct fields and that they accept the correct input type you are passing

Beyond that, if there is any other code you could share, perhaps how you are calling the postData method ? I am happy to take a look

1 Like