What's the correct way to set a Relation field when adding data to a Notion database via API?

I’m stuck trying to add a new row to my Notion database using their API. Everything works fine except for the ‘Product’ column, which is a Relation type. I can’t figure out how to specify the related item ‘Cup’ in my POST request.

Here’s a simplified version of what I’m trying:

{
  "parent": { "database_id": "abc123" },
  "properties": {
    "Item": { "title": [{ "text": { "content": "NEW ITEM" } }] },
    "Product": { "type": "relation", "relation": "Cup" },
    "Amount": { "type": "number", "number": 5 },
    "Category": { "type": "select", "select": { "name": "Inventory" } }
  }
}

The API accepts this request for other field types, but the Relation field isn’t working. I’ve looked everywhere for examples but can’t find anything clear. Can someone explain the correct format for setting a Relation field when creating a new database entry via the Notion API?

I’ve dealt with this exact issue before when working with the Notion API. The problem is that you’re trying to set the relation by name, but the API requires the page ID of the related item instead.

Here’s how you should structure the ‘Product’ field in your request:

"Product": {
  "relation": [{
    "id": "page_id_of_cup_item"
  }]
}

You’ll need to know the page ID of the ‘Cup’ item beforehand. If you don’t have it, you can use the search endpoint to find it first, then use that ID in your create page request.

Also, make sure you’re using the correct version of the API. This format works with v1 of the Notion API. If you’re still having trouble, double-check your authorization token and database ID.

Hey there! I’ve been using the Notion API for a while now, and I can totally relate to your frustration with setting up relation fields. It’s not as straightforward as it should be, that’s for sure.

Here’s what I’ve found works best: You need to provide an array of page IDs for the related items, not just the name. So your ‘Product’ field should look something like this:

"Product": {
  "relation": [{
    "id": "page_id_of_cup_item"
  }]
}

To get the page ID, you’ll probably need to query your database first to find the ‘Cup’ item. It’s a bit of a hassle, I know, but once you’ve got the hang of it, it becomes second nature.

One trick I use is to keep a mapping of item names to page IDs in my code. That way, I can quickly look up the ID I need without having to query the database every time. It’s saved me a ton of time in the long run.

Hope this helps! Let me know if you run into any other snags.