Create resource and assign relationship


#1

Hello,

I want to create users resource ( POST /users ) and assign currency relationship. Which one is correct according the specification. I was thinking about first one since it makes sense to isolate relationship from attributes, but still I’m not sure.

{
  "data": {
    "type": "users",
    "attributes": {
      "username": "username"
    },
    "relationships": {
      "currency": {
        "data": { "type": "currencies", "id": "1" }
      }
    }
  }
}

or

{
  "data": {
    "type": "users",
    "attributes": {
      "username": "username",
      "currency_id": "1"
    }
  }
}

Thanks


#2

What made you doubt?

The first example you gave is according to the spec. It states (https://jsonapi.org/format/#document-resource-object-attributes):

Although has-one foreign keys (e.g. author_id ) are often stored internally alongside other information to be represented in a resource object, these keys SHOULD NOT appear as attributes.

There is also an example (https://jsonapi.org/format/#crud-creating) of a create which looks similar to yours where a relationship is passed to assign alongside with the creation of the main resource.