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

I’m working on a project where I need to add new entries to my Notion database using their API. Everything’s going well, except for one tricky part: I can’t figure out how to set a Relation field.

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

{
  "parent": {
    "database_id": "abc123def456"
  },
  "properties": {
    "Item": {
      "title": [{"text": {"content": "PRODUCT 001"}}]
    },
    "Category": {
      "type": "relation",
      "relation": "Beverage"
    },
    "Amount": {
      "type": "number",
      "number": 5
    }
  }
}

The API call works if I remove the “Category” part, but then that field is left empty in my database. I’ve looked online for examples, but haven’t found anything that clearly explains how to handle Relations.

Has anyone successfully set a Relation field when adding data to Notion via API? Any tips or code snippets would be super helpful!

I’ve dealt with this exact issue before, and it can be a bit tricky. The key is to use the correct format for the relation field. Instead of passing the name of the related item, you need to provide its ID.

Here’s how you should structure the ‘Category’ part of your properties:

"Category": {
  "relation": [{
    "id": "page_id_of_related_item"
  }]
}

You’ll need to know the page ID of the item you’re relating to in advance. If you don’t have it, you might need to query the related database first to get the ID.

Also, make sure you’re using a POST request to the correct endpoint: /v1/pages

Hope this helps solve your problem. Let me know if you need any clarification.

hey bob, i’ve done this before. the trick is to use the page ID for the relation, not the name. it should look like this:

“Category”: {
“relation”: [{
“id”: “actual-page-id-here”
}]
}

you can find the page ID in the URL when you open the page in notion. hope this helps!

I’ve run into this issue before while integrating Notion with a custom CRM system. The trick is understanding how Notion handles relations internally. They’re not just simple text fields, but references to other pages in your workspace.

For the ‘Category’ field, you need to provide the unique identifier of the page you’re relating to, not just its name. This ID is typically a long string of characters. Here’s a revised version of your JSON that should work:

"Category": {
  "relation": [{
    "id": "f82j3k4l5m6n7o8p9"
  }]
}

Replace ‘f82j3k4l5m6n7o8p9’ with the actual ID of the ‘Beverage’ page in your Notion workspace. You can find this ID by opening the page in your browser and looking at the URL.

One caveat: make sure you have permission to relate to that specific page, or you’ll run into access errors. It took me a while to figure that out when I first implemented this!