Skip to content

Examples for creating agents using the XMTP Node SDK

License

Notifications You must be signed in to change notification settings

ephemeraHQ/xmtp-agent-examples

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

84cb592 · Mar 12, 2025
Jan 30, 2025
Jan 17, 2025
Jan 23, 2025
Mar 12, 2025
Mar 12, 2025
Mar 12, 2025
Feb 2, 2025
Mar 12, 2025
Feb 2, 2025
Jan 30, 2025
Jan 23, 2025
Jan 23, 2025
Jan 23, 2025
Jan 23, 2025
Jan 23, 2025
Jan 23, 2025
Jan 30, 2025
Jan 30, 2025
Mar 12, 2025
Jan 31, 2025
Jan 23, 2025
Mar 12, 2025
Mar 12, 2025
Mar 12, 2025

Repository files navigation

XMTP agent examples

This repository contains examples of agents that use the XMTP network.

Why XMTP?

  • End-to-end & compliant: Data is encrypted in transit and at rest, meeting strict security and regulatory standards.
  • Open-source & trustless: Built on top of the MLS protocol, it replaces trust in centralized certificate authorities with cryptographic proofs.
  • Privacy & metadata protection: Offers anonymous or pseudonymous usage with no tracking of sender routes, IPs, or device and message timestamps.
  • Decentralized: Operates on a peer-to-peer network, eliminating single points of failure.
  • Multi-agent: Allows multi-agent multi-human confidential communication over MLS group chats.

See FAQ for more detailed information.

Getting started

Environment variables

To run your XMTP agent, you must create a .env file with the following variables:

WALLET_KEY= # the private key of the wallet
ENCRYPTION_KEY= # encryption key for the local database

You can generate random keys with the following command:

yarn gen:keys

Fetching messages

There are to ways to fetch messages from a conversation, one is by starting a stream

const stream = client.conversations.streamAllMessages();
for await (const message of await stream) {
  /*You message*/
}

And by polling you can call all the messages at once, which we stored in your local database

/* Sync the conversations from the network to update the local db */
await client.conversations.sync();
// get message array
await client.conversations.messages();

Conversations can be of type Group or Dm

The new Group and Dm classes extend the Conversation class and provide specific functionality based on the conversation type.

const conversations: (Group | Dm)[] = await client.conversations.list();

for (const conversation of conversations) {
  // narrow the type to Group to access the group name
  if (conversation instanceof Group) {
    console.log(group.name);
  }

  // narrow the type to Dm to access the peer inboxId
  if (conversation instanceof Dm) {
    console.log(conversation.peerInboxId);
  }
}

Working with addresses

Because XMTP is interoperable, you may interact with inboxes that are not on your app. In these scenarios, you will need to find the appropriate inbox ID or address.

// get an inbox ID from an address
const inboxId = await getInboxIdForIdentifier({
  identifier: "0x1234567890abcdef1234567890abcdef12345678",
  identifierKind: IdentifierKind.Ethereum,
});

// find the addresses associated with an inbox ID
const inboxState = await client.inboxStateFromInboxIds([inboxId]);

interface InboxState {
  inboxId: string;
  recoveryIdentifier: Identifier;
  installations: Installation[];
  identifiers: Identifier[];
}

const addresses = inboxState.identifiers
  .filter((i) => i.identifierKind === IdentifierKind.Ethereum)
  .map((i) => i.identifier);

Web inbox

Interact with the XMTP network using xmtp.chat, the official web inbox for developers.

Examples

  • gm: A simple agent that replies to all text messages with "gm".
  • gpt: An example using GPT API's to answer messages.
  • gated-group: Add members to a group that hold a certain NFT.

See all the available examples.

Integrations

Examples integrating XMTP with external libraries from the ecosystem

  • grok: Integrate XMTP to the Grok API
  • gaia: Integrate XMTP to the Gaia API

See all the available integrations.