Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

API authentication with api key

default discord avatar
sagi86702 years ago
8

Hi, trying to access api with API key but seems to be missing something



python script to access api


import requests

base_url = 'http://localhost:3000/api'

PAYLOAD_API_KEY = '0a7913e3-9d44-4f01-af0c-aeeff5a6bacf'

messages_url = base_url + '/messages'

# get messages
headers = {
    "Authorization": f'users API-Key {PAYLOAD_API_KEY}',
}

response = requests.get(messages_url, headers=headers)
print(response.json())


Users collection


import type { CollectionConfig } from 'payload/types'

export const Users: CollectionConfig = {
  slug: 'users',
  admin: {
    useAsTitle: 'email',
  },
  auth: {
    useAPIKey: true,
  },
  fields: [
    // Email added by default
    // Add more fields as needed
  ],
}


Messages collection



import type { CollectionConfig } from 'payload/types'

export const Messages: CollectionConfig = {
  slug: 'messages',
  admin: {
    useAsTitle: 'user',
  },
  auth: false,
  fields: [
    // Email added by default
    // Add more fields as needed
    {
      name: 'user',
      type: 'text',
    },
    {
      name:'sender',
      type: 'text',
    },
    {
      name: 'content',
      type: 'text',
    }
  ],
}


Anyone?

  • default discord avatar
    markatomniux2 years ago

    Hi Sagi. Your post has only been up for a few hours and you need to remember that the majority of the Payload developer base is US based. It is also the weekend, so you may get a slower response.



    What is the error message you get when attempting to access the API with your key?

  • default discord avatar
    sagi86702 years ago

    {'errors': [{'message': 'You are not allowed to perform this action.'}]}

  • default discord avatar
    markatomniux2 years ago

    This is an issue with your RBAC, which collection are you trying to query?



    hi

    @1007251491205152830

    Could you detail what the issue was and how you fixed it for future devs 😄

  • default discord avatar
    sagi86702 years ago

    The issue was missing access configuration. I didn't really fully understand the issue nor the solution i've just shifted from the blank template to the website template and it worked



    @191776538205618177
  • default discord avatar
    tansan.ethlast year
    @1007251491205152830

    do u have an example?

  • default discord avatar
    sagi8670last year

    Look at the user collection on web template

  • default discord avatar
    markatomniuxlast year
    @108781176075898880

    I think what

    @1007251491205152830

    is trying to say is they did not have their access control settings configured correctly for the collection.



    https://payloadcms.com/docs/access-control/overview

    the website template that payload uses configures these access control settings for you. But it's still important to understand how the access control system functions in Payload

  • default discord avatar
    tansan.ethlast year

    I just made everything true for now to see if it works, but I am unsure if i am missing something else?



    https://discord.com/channels/967097582721572934/1257494555209764964/1257494555209764964

    I followed the example in the link here. Could you take a look to see if that makes sense?

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.