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.

duplicate via the rest api?

default discord avatar
hendrik012 years ago
8

Did not find anything about this in the docs. Is it possible to duplicate a document via the rest api?

  • default discord avatar
    notchr2 years ago

    By default, I don't think so, but you can easily add a custom endpoint to your REST api.

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

    I imagine you'd look up an existing post by ID, then you would copy the field data and create a new post and let the ID populate.

  • default discord avatar
    hendrik012 years ago
    @1049775120559898725

    yeah, have already done this, but thought it would be nice not to produce two endpoinst for the same behaviour 🙂

  • default discord avatar
    notchr2 years ago

    That's true, if the aren't any side effects, you could create a reusable utility to do this



    But i agree, built in duplicate would be nice



    🙂

  • discord user avatar
    jarrod_not_jared
    2 years ago

    Hey

    @441722987817730058

    just catching up here. I think the custom endpoint is the right solution for now, we are not sure if this is something we will provide out of the box yet. A couple things you will want to keep in mind when doing this:


    const accessibleDoc = await payload.findByID({
      collection: 'pages',
      id: req.query.id,
      showHiddenFields: true,
      overrideAccess: false,
      user: req.user,
    })


    You will want to pass the user through, turn off the overrideAccess, and show hidden fields. Then you should check if a doc comes back, if it does, the user has access and should be allowed to duplicate, if not then the user does not have access and should return an error. You will want to include showHiddenFields so all fields are duplicated onto the new doc

  • default discord avatar
    hendrik012 years ago

    yeah did exactly this

  • discord user avatar
    jarrod_not_jared
    2 years ago

    perfect

  • default discord avatar
    hendrik012 years ago

    thanks! 🙂

  • discord user avatar
    jarrod_not_jared
    2 years ago

    np 🙂

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.