This endpoint allows you to switch to a different tab in a Chrome browser window. You can specify the target tab either by its index (order in the tab bar) or by providing a tab ID.
{
"index": "0" // (string or number) Zero-based index of the target tab
// or
"id": "tab-123" // (string) Tab ID of the target tab
}
| Parameter | Type | Required | Description |
|---|---|---|---|
| index | string or number | No* | The zero-based index of the tab to switch to (e.g., 0 for first tab, 1 for second tab). |
| id | string | No* | The ID of the tab to switch to. |
index or id must be provided, but not both.
{
"success": boolean, // Whether the operation was successful
"message": "string" // Result message
}
| Field | Type | Description |
|---|---|---|
| success | boolean | Indicates whether the tab switch operation was successful. |
| message | string | A message describing the outcome of the operation, for example "Switched to tab index 0". |
import requests
import json
def switch_chrome_tab(tab_identifier, api_key):
"""
Switch to a different tab in Chrome.
Args:
tab_identifier: Either the index number of the tab or the tab ID string
api_key: Your API key for authentication
Returns:
dict: The response from the server
"""
url = "http://localhost:5000/tools-api/chrome/switch-tab"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
# Determine if the identifier is an index or ID
if isinstance(tab_identifier, int):
data = {
"index": tab_identifier
}
else:
data = {
"id": tab_identifier
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
# Example usage
if __name__ == "__main__":
API_KEY = "your_api_key_here"
# Example 1: Switch using index
result = switch_chrome_tab(1, api_key=API_KEY)
if result["success"]:
print(f"Switched to tab: {result['message']}")
else:
print(f"Error: {result['message']}")
# Example 2: Switch using tab ID
result = switch_chrome_tab("ABC123", api_key=API_KEY)
if result["success"]:
print(f"Switched to tab: {result['message']}")
else:
print(f"Error: {result['message']}")
interface SwitchTabRequest {
index?: number;
id?: string;
}
interface SwitchTabResponse {
success: boolean;
message: string;
}
/**
* Switch to a different tab in Chrome.
*
* @param tabIdentifier - Either the index number of the tab or the tab ID string
* @param apiKey - Your API key for authentication
* @returns A promise that resolves to the switch tab response
*/
async function switchChromeTab(
tabIdentifier: number | string,
apiKey: string
): Promise {
const url = 'http://localhost:5000/tools-api/chrome/switch-tab';
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
};
let requestBody: SwitchTabRequest;
if (typeof tabIdentifier === 'number') {
requestBody = {
index: tabIdentifier
};
} else {
requestBody = {
id: tabIdentifier
};
}
const response = await fetch(url, {
method: 'POST',
headers,
body: JSON.stringify(requestBody)
});
return await response.json();
}
// Example usage
(async () => {
const API_KEY = 'your_api_key_here';
try {
// Example 1: Switch using index
const result1 = await switchChromeTab(1, API_KEY);
if (result1.success) {
console.log(`Switched to tab: ${result1.message}`);
} else {
console.log(`Error: ${result1.message}`);
}
// Example 2: Switch using tab ID
const result2 = await switchChromeTab("ABC123", API_KEY);
if (result2.success) {
console.log(`Switched to tab: ${result2.message}`);
} else {
console.log(`Error: ${result2.message}`);
}
} catch (error) {
console.error('Error switching tab:', error);
}
})();
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class SwitchTabRequest
{
public int? Index { get; set; }
public string Id { get; set; }
}
public class SwitchTabResponse
{
public bool Success { get; set; }
public string Message { get; set; }
}
public class ToolsServerClient
{
private readonly HttpClient _httpClient;
private readonly string _apiKey;
public ToolsServerClient(string apiKey)
{
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri("http://localhost:5000/");
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
_apiKey = apiKey;
}
///
/// Switch to a different tab in Chrome.
///
/// Either the index number of the tab or the tab ID string
/// Response containing the result and information about the tab that was switched to
public async Task SwitchChromeTabAsync(object tabIdentifier)
{
var request = new SwitchTabRequest();
if (tabIdentifier is int index)
{
request.Index = index;
}
else if (tabIdentifier is string id)
{
request.Id = id;
}
var jsonContent = JsonSerializer.Serialize(request);
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("tools-api/chrome/switch-tab", content);
var responseBody = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize(responseBody, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
}
}
// Example usage
class Program
{
static async Task Main(string[] args)
{
string apiKey = "your_api_key_here";
var client = new ToolsServerClient(apiKey);
try
{
// Example 1: Switch using index
var result1 = await client.SwitchChromeTabAsync(1);
Console.WriteLine($"Switched to tab: {(result1.Success ? result1.Message : "Failed")}");
// Example 2: Switch using tab ID
var result2 = await client.SwitchChromeTabAsync("ABC123");
Console.WriteLine($"Switched to tab: {(result2.Success ? result2.Message : "Failed")}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
This example demonstrates switching to a tab by its index:
// Request
POST /tools-api/chrome/switch-tab
{
"index": 2
}
// Response
{
"success": true,
"message": "Switched to tab index 2"
}
This example shows switching to a tab by its ID:
// Request
POST /tools-api/chrome/switch-tab
{
"id": "ABC123"
}
// Response
{
"success": true,
"message": "Switched to tab with ID 'ABC123'"
}