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.

Adding custom data in the API response

default discord avatar
raburarulast year
3

Hello Question - I'm not really sure where to look. May someone point me in the right direction to find relevant docs?



I have a field


{
    name: 'location',
    type: 'text',
    label: "Location",
    required: true,
},

And of course with the field filled out shows up as this in the API



"location": "London",


For some basic example lets say I want to return a value but is not something seen or edited in the collection for this. Is hooks where I should go? I want the result to look like such:



"location": "London",
  "customHiddenStuff": "Foo",
  "customHiddenNumber": "123",
  etc



Where might I find docs relevant to this? Thank you

  • default discord avatar
    oldmanpewpewlast year

    Zushi - ive used the afterRead hook for things like this. In my case i was removing some data - but no reason it couldnt also be used to add data

  • default discord avatar
    brianjmlast year
    afterRead

    is perfect for transforming data that does not need to be saved.



    I'm not sure if you can add data that is not part of the schema, however.

  • default discord avatar
    raburarulast year

    Thank you!

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.