XMTP is a communication protocol for Web3, allowing you to build apps that communicate with every wallet in the world. In this guide you will learn to programmatically send XMTP messages to users when they sign up using a wallet.

Prerequisites

Step 1: Access the wallet address

As long as you have completed the serverless webhook guide, you’ll know how to access the email of a user within your serverless function. However in this instance, we need a wallet address.

A new user will have a wallet in two instances:

  1. They signup using an EOA
  2. They receive an embedded wallet

You can learn more about the different kinds of signup you can enable in the following section of the docs: https://docs.dynamic.xyz/sign-in-sign-up/overview. Let’s assume for now that you have both embedded wallets enabled, and EOA signups enabled so that the user object is guaranteed to have a wallet address.

When a user signs up, you can access their wallet address through the Verified Credentials array (reference here):

const verifiedCredentials = event.data?.verifiedCredentials;

if (!verifiedCredentials) {
  return {
    statusCode: 400,
    body: JSON.stringify({
      error: "No verified credentials found",
    }),
  };
}

const blockchainVC = verifiedCredentials.find((vc) => vc.type === "blockchain");

if (!blockchainVC) {
  return {
    statusCode: 400,
    body: JSON.stringify({
      error: "No blockchain VC found",
    }),
  };
}

const address = blockchainVC.address;

Step 2: Send the XMTP message

We will need to install the XMTP library for NodeJS first:

npm i @xmtp/xmtp-js

Then we can import the library and send a message:

// xmtp.js
const { Client } = require("@xmtp/xmtp-js");

const sendMessage = (address) => {
    try {
        const conv = await xmtp.conversations.newConversation(address);
        return  await conv.send("gm");
    } catch(e) {
        console.log(e)
    }
}

export default sendMessage;

Step 3: Call the function

Going back to our Serverless guide at Step 3, all we have to do now is add in our XMTP function call:

// index.js

const sendMessage = require("./xmtp.js");

module.exports.handler = async (event) => {
  const verifiedCredentials = event.data?.verifiedCredentials;

  if (!verifiedCredentials) {
    return {
      statusCode: 400,
      body: JSON.stringify({
        error: "No verified credentials found",
      }),
    };
  }

  const blockchainVC = verifiedCredentials.find(
    (vc) => vc.type === "blockchain"
  );

  if (!blockchainVC) {
    return {
      statusCode: 400,
      body: JSON.stringify({
        error: "No blockchain VC found",
      }),
    };
  }

  const address = blockchainVC.address;

  await sendMessage(address);

  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: "Go Serverless v3.0! Your function executed successfully!",
        input: event,
      },
      null,
      2
    ),
  };
};

You can then test and redeploy your function as in Step 4 of the serverless guide, and you’re done!