Can I access the payload object from a custom endpoint on a collection?

default discord avatar
notchr
3 months ago
5

Wanted to access the Payload object on a custom endpoint on a collection to send an email through Payload. Is this better suited for the local API?

  • default discord avatar
    jarrod69420
    3 months ago

    What do you mean exactly? The req should have payload object on it right? And in that case you would be able to us the local API to send the email

  • default discord avatar
    notchr
    3 months ago

    Ah, for some reason I didn't realize the payload object is on the req!



    Thanks @jarrod69420



    For anyone who finds this, I did...



      endpoints: [
        {
          path: "/verify",
          method: "post",
          handler: async (req, res, next) => {
            try {
              const msg: Message = {
                from: "noreply@example.com",
                to: req.body.email,
                subject: "Example",
                html: `The id posted was ${req.body.id}`,
              };
              await req.payload.sendEmail(msg);
            } catch (error) {
              res.json({
                ok: false,
                error: "Error while sending verification email.",
              });
            }
          },
        },
      ],
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.