← 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 fresh test channel named api-key-test-<random> (a new one on every page load) with a random password. 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 an 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 a regular message (persisted)

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

3. Send an ephemeral message (not persisted)

// 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: 'YOUR_CHANNEL_PASSWORD',
            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('pagehide', () => agent.disconnect());
}

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