Skip to main content
The Agent Platform JavaScript SDK (AgP JS SDK) gives you a simple, powerful way to discover, select, and run AI agents directly from your application. Whether you’re exploring the web, automating research, or running multi-step workflows, the SDK makes it easy to find the right agent for any task — and have it execute automatically. In the example below, we’ll walk through finding the best agent for a specific objective using built-in SDK methods. You’ll see how the SDK identifies available agents, recommends the most suitable one, and runs it to complete your goal — all in just a few lines of code.

Finding the best agent

Situation: I want the agent to find noise-cancelling headphones on the web but have no idea where to start, what to look for, or, even, which agent is best suited to the task. Problem: How do I find out which agents are available to use? How do I decide on the right agent to use? And how do I get the agent to automatically execute my task based on its recommendation? Solution: The AgP JS SDK uses the agent.listAgents(), agent.getBestAgent(), and agent.run()commands asynchronously to list all available agents, retrieve the best agent based on the task described, and run the proposed agent to find information on noise-cancelling headphones on the web.

Step 1: Initialize the SDK

Start by initializing the SDK and setting up authentication via Portal-H by adding the following:
<button id="loginBtn" style="display:none;">Login to Portal-H</button>

<script src="https://unpkg.com/agp-js-sdk"></script>
<script>
(async () => {
  const agent = await AGP.WebAgent.init({
    authCallback: (login) => {
      const btn = document.getElementById('loginBtn');
      btn.style.display = 'block';
      btn.onclick = login;
    }
  });

Step 2: List available agents

Add agent.listAgents() function to retrieve a list of agents available in your account.
  const agents = await agent.listAgents();
  console.log('Available agents:', agents);

Step 3: Select the best agent automatically

You can automatically select the most appropriate agent for your task type using the agent.getBestAgent() function.
  const bestAgent = await agent.getBestAgent('web');
  console.log('Best agent selected:', bestAgent);

Step 4: Run a task

Add theagent.run()function and define a task that includes both an **Objective **and a startUrl.
const task = await agent.run('Search for best noise-cancelling headphones', {
  startUrl: 'https://google.com'
});

Step 5: Attach a listener

Add the following code to view task updates in real time.
task.onUpdate((event) => {
  console.log(event.type, event.data);
});

Step 6: Wait for completion

To be notified in real time when your task has finished executing, add the following code:
await task.waitForCompletion();

Step 7: Put it all together and run

Here’s a complete example that includes all previous steps, assembled and ready to run:
Browser example
<button id="loginBtn" style="display:none;">Login to Portal-H</button>

<script src="https://unpkg.com/agp-js-sdk"></script>
<script>
(async () => {
  // Initialize SDK and set up login button
  const agent = await AGP.WebAgent.init({
    authCallback: (login) => {
      const btn = document.getElementById('loginBtn');
      btn.style.display = 'block';
      btn.onclick = login;
    }
  });

  // Step 2: List available agents
  const agents = await agent.listAgents();
  console.log('Available agents:', agents);

  // Step 3: Select the best agent automatically
  const bestAgent = await agent.getBestAgent('web');
  console.log('Best agent selected:', bestAgent);

  // Step 4: Run a test task
  const task = await agent.run('Search for best noise-canceling headphones', {
    startUrl: 'https://www.google.com'
  });

  // Listen for real-time updates on the task
  task.onUpdate((event) => {
    console.log(event.type, event.data);
  });

  await task.waitForCompletion();
})();
</script>

Outcome

The AgP JS SDK, running the above code asyncronously, will follow a sequence that:
  • Initialize the SDK and authenticate through Portal-H.
  • List available agents, for example:
    nameagent_identifierdescription
    surferhsurferhGood agent for navigation and retrieval
    surferh-highsurferh-highBest performing agent, but slower
    surferh-fastsurferh-fast’Fastest agent, but less accurate
  • Select the best agent for the task, for example: agent_identifier: “surferh” description: “Good agent for navigation and retrieval” name: “surferh”
  • Generate an accurate and detailed response, for example:
    Best Noise-Canceling Headphones (2025)
    According to Bing's editorial summary, some of the highest-rated current noise-canceling headphones are:
    
    Bose QuietComfort Ultra Headphones: High-end, excellent sound quality
    Sony WH-1000XM5: Over-ear, superior sound and comfort
    Sonos Ace: Newly released, impressive sound and features
    QCY H3 Pro: Affordable, good performance for price
    Bose QuietComfort Ultra Earbuds: True wireless, exceptional noise-canceling
    AirPods Max: Premium over-ear, outstanding sound and cancellation
    For further details and current prices, see the Bing noise-canceling headphone search results.
    
    These models consistently appear at the top of comparison lists for 2025 and are recommended for their active noise-canceling capabilities and audio performance.
    
I