Solved! How do I access the webhook payload in an Autotask

I need some help, please. I have an Autotask with a relay. I am calling it with a webhook from my app. It safeMints an NFT with URI Storage. I send the URI and Recipient in the JSON body of my fetch.

How do I access the payload (URI and Recipient) in my Autotask? I have tried a few different ways:

To get the json body..
const formData = JSON.parse(params.request.body)
or
const formData = JSON.parse(event.request.body)

Then
const URI = formdata.uri;
const RECIPIENT = formdata.recipient;

Errors: request or event is not defined

By the way, If I hard code the uri and the recipient in the Autotask it succeeds.

Here’s my Autotask code:

const ABI = [`function safeMint(address to, string uri) public`];

const formdata = JSON.parse(params.request.body);
const URI = formdata.uri;
const RECIPIENT = formdata.recipient;
  
const ADDRESS = "0xb56C22a246F39ac92fAFae8EAA82996f00ef5F19";

const { ethers } = require("ethers");
const {
  DefenderRelaySigner,
  DefenderRelayProvider,
} = require("defender-relay-client/lib/ethers");

/**   Mint an NFT for the recipient */

async function main(signer, recipient) {
  const nft = new ethers.Contract(ADDRESS, ABI, signer);
  const tx = await nft.safeMint(recipient, URI);
  console.log(`Minted an NFT for ${recipient} in ${tx.hash}`);
}

exports.handler = async function (params) {
  const provider = new DefenderRelayProvider(params);
  const signer = new DefenderRelaySigner(params, provider, { speed: "fast" });
  console.log(`Using relayer ${await signer.getAddress()}`);
  await main(signer, RECIPIENT);
};

I got it working! I was not using export.handler properly. Here is my Autotask code and it works now!

const ABI = [`function safeMint(address to, string uri) public`];

const ADDRESS = "0xb56C22a246F39ac92fAFae8EAA82996f00ef5F19";

const { ethers } = require("ethers");
const {
  DefenderRelaySigner,
  DefenderRelayProvider,
} = require("defender-relay-client/lib/ethers");
// const { data } = require("autoprefixer");

/**   Mint an NFT for the recipient */

async function main(signer, recipient, uri) {
  const nft = new ethers.Contract(ADDRESS, ABI, signer);
  const tx = await nft.safeMint(recipient, uri);
  console.log(`Minted an NFT for ${recipient} in ${tx.hash}`);
}

exports.handler = async function (event) {
  const provider = new DefenderRelayProvider(event);
  const signer = new DefenderRelaySigner(event, provider, { speed: "fast" });
  const recipient = event.request.body.recipient;
  const uri = event.request.body.uri;
  console.log(`Using relayer ${await signer.getAddress()}`);
  await main(signer, recipient, uri);
};
1 Like