Help topics

API returns 403 Forbidden

Symptoms

The API answers with HTTP status 403 and a body like this:

{"error":{"code":"forbidden","message":"you may not create book","retry":false}}

The message field tells you which check failed. These are the messages you can see:

  • you may not read <type>
  • you may not create <type>, you may not update <type> or you may not delete <type>
  • you may not see deleted <type>
  • you may not see the fields attached to this item
  • administrators only

Retrying the same request does not help. retry is false.

Cause

Check these causes in this order.

  • The model's access rules do not allow the action. A request without a valid key and without a matching WordPress user gets you may not <action> <type>. Morphic allows an action when an administrator makes the request without a key, or when a valid key allows it. The rule must match the user: public, authenticated, role:<slug>, or a WordPress capability such as edit_posts that the user has.
  • The request has no key, or the key cannot write. A key with Read only is allowed to read. Create, update and delete need a key with Read & write, unless an administrator sends the request with no key. A key only works for the models it is limited to.
  • The key is malformed or unknown. Morphic reads only keys that start with mk_. Any other value, or a key that does not exist, is treated as no key, so the request is anonymous.
  • The request asks for deleted entries without delete permission. The bin query parameter needs the delete permission. Only an administrator without a key, a valid key with write access, or a user allowed to delete can see the bin.
  • The request asks for attached fields without edit permission. Attached fields need the WordPress capability to edit the post, user or term. API keys do not count for this check.
  • The request is for a key route. Only an administrator can list, create or revoke API keys. A key cannot do this.

Fix

If the model's access rules do not allow the action

  1. Open the model's Access tab and select the box for the action and the group that matches the caller. See Set who can read and change a model.
  2. Select Save changes.
  3. Send the request again.

If the request has no key, or the key cannot write

  1. Send the key in the X-Morphic-Key header, or in the Authorization header as Bearer mk_….
  2. For create, update or delete, check that the key shows Read & write in the Can column of the API keys tab. Read-only keys cannot write.
  3. Check that the key's model list includes the model in the request. See Create and revoke API keys.
  4. Send the request again.

If the key is malformed or unknown

  1. Check that the key starts with mk_.
  2. If you cannot find the full key you copied when you created it, create a new key.
  3. Send the request again.

If the request asks for the bin

  1. Use a key with Read & write for that model, or a WordPress user who is allowed to delete entries of that model.
  2. Send the request again with bin=1 in the query.

If the request asks for attached fields

  1. Sign in as a WordPress user who can edit the post, user or term that the fields are attached to.
  2. Send the request again.

If the message is administrators only

  1. Make the request as a WordPress administrator. API keys cannot list, create or revoke keys.

Still not working?

Collect the request method and URL, and the response body. Note whether a key was sent and in which header. Note the role of the WordPress user, if there is one, and the model's access rules. Note the Morphic version, which the Settings screen shows. The error message tells you which check failed. Send these to the administrator of the Morphic site.

Contact us through the contact form with your Morphic version and the exact error text.