← Back to SDK Home

🔑 Test Your API Key

Quickly verify your API key works by testing real-time messaging between two agents

1ī¸âƒŖ Enter Your API Key

đŸ”ĩ Agent 1 (Alice)

● Disconnected

đŸŸĸ Agent 2 (Bob)

● Disconnected
✅

API Key Working!

Both agents connected and can exchange messages successfully.

📋 How It Works

  1. Enter your API Key in the input field above
  2. Click "Connect Both Agents" to establish connections
  3. Once connected, click "Send Message" from either agent
  4. Watch the message appear in the other agent's log
  5. If messages are exchanged successfully, your API key is working! ✅

💡 Tip: Both agents join a test channel called api-key-test-channel with password test123. This is for testing purposes only.

⚡ Ephemeral Messages: By default, messages are sent as ephemeral (not stored in DB). Uncheck the checkbox to send regular messages that are stored.

đŸ’ģ Code Reference - How This Works

Below is the actual source code used in this page. Copy and adapt for your own projects.

1ī¸âƒŖ Create and Connect Agent

// Create new agent instance
const agent = new AgentConnection();

// Set up message listener BEFORE connecting
agent.addEventListener('message', (ev) => {
    const messages = ev.response?.data || [];  // Array of messages
    messages.forEach(msg => {
        if (msg && msg.type === 'chat-text') {
            console.log(`Received: ${msg.content} from ${msg.from}`);
        }
    });
});

// Connect to the messaging service
await new Promise((resolve, reject) => {
    const timeout = setTimeout(() => reject(new Error('Timeout')), 10000);

    agent.addEventListener('connect', (ev) => {
        clearTimeout(timeout);
        const resp = ev.response || {};
        if (resp.status === 'error') {
            reject(new Error(resp.statusMessage || 'Failed'));
        } else {
            resolve();
        }
    });

    agent.connect({
        api: 'https://hmdevonline.com/messaging-platform/api/v1/messaging-service',
        apiKey: 'YOUR_API_KEY_HERE',
        channelName: 'my-test-channel',
        channelPassword: 'my-password',
        agentName: 'MyAgent-' + Date.now(),
        autoReceive: true  // Automatically receive messages
    });
});

2ī¸âƒŖ Send Regular Message (Stored in DB)

// Send a regular message (stored in database)
agent.sendMessage({
    content: 'Hello, World!',
    type: 'chat-text',
    ephemeral: false  // false = stored in DB
});

3ī¸âƒŖ Send Ephemeral Message (NOT Stored in DB)

// Send an ephemeral message (NOT stored in database)
agent.sendMessage({
    content: 'Temporary message',
    type: 'chat-text',
    ephemeral: true  // true = NOT stored, real-time only
});

// Use case: WebRTC signaling, temporary status updates, etc.

4ī¸âƒŖ Disconnect Agent

// Disconnect when done
agent.disconnect();

// Or on page unload (only disconnects when page actually closes)
window.addEventListener('unload', () => {
    if (agent) agent.disconnect();
});

5ī¸âƒŖ Complete Example

// Complete working example
async function testMessaging() {
    // 1. Create agent
    const agent = new AgentConnection();

    // 2. Setup message handler
    agent.addEventListener('message', (ev) => {
        const messages = ev.response?.data || [];
        messages.forEach(msg => {
            if (msg.type === 'chat-text') {
                console.log(`📩 ${msg.from}: ${msg.content}`);
            }
        });
    });

    // 3. Connect
    await new Promise((resolve, reject) => {
        const timeout = setTimeout(() => reject(new Error('Timeout')), 10000);
        agent.addEventListener('connect', (ev) => {
            clearTimeout(timeout);
            ev.response?.status === 'error' ? reject(ev.response) : resolve();
        });
        agent.connect({
            api: 'https://hmdevonline.com/messaging-platform/api/v1/messaging-service',
            apiKey: 'YOUR_API_KEY',
            channelName: 'test-' + Date.now(),
            channelPassword: 'test123',
            agentName: 'TestAgent',
            autoReceive: true
        });
    });

    console.log('✅ Connected!');

    // 4. Send messages
    agent.sendMessage({
        content: 'Hello!',
        type: 'chat-text',
        ephemeral: true  // Default: ephemeral (not stored)
    });

    // 5. Cleanup (only disconnects when page actually closes)
    window.addEventListener('unload', () => agent.disconnect());
}

// Run it
testMessaging().catch(console.error);