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.

REST api request specific data

default discord avatar
roadbl0ck_0last year
16

Is it possible to request specific data or fields using REST API?


Like request for title price and slug rather than all the data in list.

  • default discord avatar
    zed0547last year

    Hey

    @1126955972439580776

    ,



    So generally speaking the issue you are experiencing is called

    Overfetching

    which isn't really an issue unique to Payload. It is simply a limitation of RESTful API's. You have a few options of how to proceed here.



    You could create a custom endpoint in your collection which would return only the fields you want:

    https://payloadcms.com/docs/beta/rest-api/overview#custom-endpoints

    You could also use the GraphQL endpoint which comes with Payload as GraphQL is seen as a direct solution to

    Overfetching

    /

    Underfetching

    by allowing you to specify specific fields to return.

    https://payloadcms.com/docs/beta/graphql/overview

    If neither of these solutions work for you, you could also create a collection

    afterRead

    hook that strips the outgoing response of unnecessary fields, although I would not recommend this approach.

    https://payloadcms.com/docs/beta/hooks/collections#afterread

    Lastly, you could use the

    Search

    plugin and override the fields that get indexed to only the fields you require and simply use this collection as a proxy.

    https://payloadcms.com/docs/beta/plugins/search#options

    I would not recommend this approach either, but it may work for your use-case.

  • default discord avatar
    roadbl0ck_0last year
    @654031862146007055

    very nice explanation


    I shall go with the custom endpoint



    Thank you

  • default discord avatar
    zed0547last year

    My pleasure!

  • default discord avatar
    roadbl0ck_0last year
    @654031862146007055

    need some more help



    Here is the endpoint



    endpoints: [


    {


    path: '/list',


    method: 'get',


    handler: async (req) => {


    return Response.json({


    message: 'this is endpoint'


    })


    }


    }


    ],



    How I can add the field I want to return?

  • default discord avatar
    zed0547last year

    That would entirely depend on what you're trying to achieve here. You want to perform a query? You can use

    req.payload

    to get access to the local api.



    Have a look here for more:

    https://payloadcms.com/docs/beta/rest-api/overview#helpful-tips

    Also look at the example above in that link

  • default discord avatar
    roadbl0ck_0last year

    I want to perform get request to get the product list with title, slug and price.

  • default discord avatar
    zed0547last year

    Okay, so you would use the local api to fetch your list, then you would loop through the items and strip them of unnecessary fields and return the result.



    ...
    handler: async (req) => {
        const data = await req.payload.find({
          collection: <your-collection-slug>,
        })
    
        // your logic here
    
        return Response.json({
            message: 'this is endpoint',
            result: <your-sanitized-data-here>
        })
    }
    ...


    See Local API docs here:

    https://payloadcms.com/docs/beta/local-api/overview
  • default discord avatar
    roadbl0ck_0last year
    @654031862146007055

    it worked but I am failing to write a logic to get the specific fields and return it.



    I tried to use


    const result = data.docs.map(item => item.title)



    But it is returning title value like “product title” and not key value pair object as json


    Like {“title”:product title}



    Any idea, how I can do it?

  • default discord avatar
    zed0547last year

    Yeah, so what you want is something like this:


    const result = data.docs.map(item => ({
      title: item.title,
      slug: item.slug,
      price: item.price,
    ...
    }))


    This will return an array of objects where each object has those fields.

  • default discord avatar
    roadbl0ck_0last year

    You are awesome man thank you for helping me



    It worked perfectly the way I want

  • default discord avatar
    zed0547last year

    My pleasure

  • default discord avatar
    roadbl0ck_0last year
    @654031862146007055

    there is one issue I just noticed


    There is no pagination in this endpoint



    No worries fixed it



    @654031862146007055

    is it good practice to handle pagination in endpoint?



    const data = await req.payload.find({


    collection: 'products',


    limit: 1,


    page: Number(req.searchParams.get('page’))


    })



    It is working but don’t know if it is good or there is better way

  • default discord avatar
    zed0547last year

    Seems fine to me!



    One thing you may consider is implementing a

    limit

    query parameter so that your

    limit: 1

    can be overriden to get more data

  • default discord avatar
    roadbl0ck_0last year

    Yes will do that

  • default discord avatar
    paradox5270last year

    To extend on this, you can further reduce over fetching data of the database by directly accessing the database adapters (req.payload.db.drizzle for sql databases) and do raw sql statements.



    Also I’m sure

    @423216344302092288

    is working on the select feature similar to how Strapi does it.

  • default discord avatar
    ritsu0455last year

    Yeah, in fact it's done already!

    https://github.com/payloadcms/payload/pull/8550
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.