API Documentation

Complete reference for all browser extension APIs and methods

Core APIs

Essential APIs for extension functionality

chrome.runtime

STABLE

Use the chrome.runtime API to retrieve the background page, return details about the manifest, and listen for and respond to events in the extension lifecycle.

sendMessage()

Send a message to event listeners within your extension or a different extension.

SYNTAX

chrome.runtime.sendMessage(
  extensionId?: string,
  message: any,
  options?: object,
  callback?: function
)

PARAMETERS

extensionId
string (optional)

The ID of the extension to send the message to.

message
any

The message to send. This can be any JSON-serializable object.

callback
function (optional)

Function called when a response is received.

EXAMPLE

// Send message
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
  console.log(response.farewell);
});

// Listen for message
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  console.log(request.greeting);
  sendResponse({farewell: "goodbye"});
});

getURL()

Converts a relative path within an extension to a fully-qualified URL.

SYNTAX

chrome.runtime.getURL(path: string)

EXAMPLE

let url = chrome.runtime.getURL('images/icon.png');
console.log(url); // chrome-extension://[extension-id]/images/icon.png

Events

onInstalled

Fired when the extension is first installed, updated, or Chrome is updated.

chrome.runtime.onInstalled.addListener(function(details) {
  console.log('Extension installed:', details.reason);
  if(details.reason === "install"){
    // First install
  } else if(details.reason === "update"){
    // Extension updated
  }
});
onMessage

Fired when a message is sent from an extension process or content script.

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  if (request.action === "getData") {
    sendResponse({data: "Response data"});
  }
  return true; // Required for async response
});

chrome.storage

STABLE

Use the chrome.storage API to store, retrieve, and track changes to user data.

Permission Required

You must declare the "storage" permission in the manifest to use this API.

"permissions": ["storage"]

storage.sync.set()

Stores one or more items in the storage area, or updates existing items.

SYNTAX

chrome.storage.sync.set(items: object, callback?: function)

EXAMPLE

// Save data
chrome.storage.sync.set({key: 'value'}, function() {
  console.log('Value is set');
});

// Save multiple values
chrome.storage.sync.set({
  username: 'John',
  age: 30,
  preferences: {theme: 'dark'}
}, function() {
  console.log('Settings saved');
});

storage.sync.get()

Retrieves one or more items from the storage area.

EXAMPLE

// Get single value
chrome.storage.sync.get(['key'], function(result) {
  console.log('Value is: ' + result.key);
});

// Get multiple values
chrome.storage.sync.get(['username', 'age'], function(result) {
  console.log('Username:', result.username);
  console.log('Age:', result.age);
});

// Get all values
chrome.storage.sync.get(null, function(items) {
  console.log('All items:', items);
});

storage.sync.remove()

Removes one or more items from the storage area.

EXAMPLE

// Remove single key
chrome.storage.sync.remove('key', function() {
  console.log('Key removed');
});

// Remove multiple keys
chrome.storage.sync.remove(['key1', 'key2'], function() {
  console.log('Keys removed');
});
Storage Areas
storage.sync

Data synced across devices

Limit: 100KB

storage.local

Local device storage

Limit: 10MB

storage.session

Session-based storage

Cleared on restart

chrome.tabs

STABLE

Use the chrome.tabs API to interact with the browser's tab system. You can use this API to create, modify, and rearrange tabs in the browser.

query()

Gets all tabs that have the specified properties.

EXAMPLE

// Get current active tab
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  let currentTab = tabs[0];
  console.log('Current URL:', currentTab.url);
});

// Get all tabs
chrome.tabs.query({}, function(tabs) {
  console.log('Total tabs:', tabs.length);
});

// Get tabs by URL pattern
chrome.tabs.query({url: '*://example.com/*'}, function(tabs) {
  console.log('Matching tabs:', tabs);
});

create()

Creates a new tab.

EXAMPLE

// Create new tab
chrome.tabs.create({url: 'https://example.com'});

// Create tab with options
chrome.tabs.create({
  url: 'https://example.com',
  active: true,
  pinned: false,
  index: 0
}, function(tab) {
  console.log('Tab created:', tab.id);
});

update()

Modifies the properties of a tab.

EXAMPLE

// Update tab URL
chrome.tabs.update(tabId, {url: 'https://newurl.com'});

// Mute/unmute tab
chrome.tabs.update(tabId, {muted: true});

// Pin/unpin tab
chrome.tabs.update(tabId, {pinned: true});

remove()

Closes one or more tabs.

EXAMPLE

// Close single tab
chrome.tabs.remove(tabId);

// Close multiple tabs
chrome.tabs.remove([tabId1, tabId2, tabId3]);

chrome.windows

STABLE

Use the chrome.windows API to interact with browser windows. You can use this API to create, modify, and rearrange windows.

create()

Creates a new browser window.

EXAMPLE

// Create new window
chrome.windows.create({
  url: 'https://example.com',
  type: 'popup',
  width: 800,
  height: 600
});

// Create incognito window
chrome.windows.create({
  url: 'https://example.com',
  incognito: true
});

getAll()

Gets all windows.

EXAMPLE

chrome.windows.getAll({populate: true}, function(windows) {
  windows.forEach(function(window) {
    console.log('Window ID:', window.id);
    console.log('Tab count:', window.tabs.length);
  });
});

UI APIs

APIs for user interface interactions

chrome.action

MANIFEST V3

Use the chrome.action API to control the extension's icon in the browser toolbar.

setIcon()

Sets the icon for the extension action.

EXAMPLE

chrome.action.setIcon({
  path: {
    "16": "images/icon16.png",
    "48": "images/icon48.png",
    "128": "images/icon128.png"
  }
});

setBadgeText()

Sets the badge text for the extension action.

EXAMPLE

// Set badge text
chrome.action.setBadgeText({text: '5'});

// Set badge background color
chrome.action.setBadgeBackgroundColor({color: '#FF0000'});

setTitle()

Sets the title of the extension action (tooltip text).

EXAMPLE

chrome.action.setTitle({title: 'Click to activate'});

chrome.notifications

STABLE

Use the chrome.notifications API to create rich notifications using templates and show these notifications to users.

create()

Creates and displays a notification.

EXAMPLE

chrome.notifications.create({
  type: 'basic',
  iconUrl: 'icon48.png',
  title: 'Notification Title',
  message: 'This is the notification message',
  priority: 2
}, function(notificationId) {
  console.log('Notification created:', notificationId);
});

// Progress notification
chrome.notifications.create({
  type: 'progress',
  iconUrl: 'icon48.png',
  title: 'Download',
  message: 'Downloading file...',
  progress: 50
});

chrome.contextMenus

STABLE

Use the chrome.contextMenus API to add items to the browser's context menu.

create()

Creates a new context menu item.

EXAMPLE

// Create menu item
chrome.contextMenus.create({
  id: 'menu1',
  title: 'Search with Extension',
  contexts: ['selection']
});

// Listen for clicks
chrome.contextMenus.onClicked.addListener(function(info, tab) {
  if (info.menuItemId === 'menu1') {
    console.log('Selected text:', info.selectionText);
  }
});

Network APIs

APIs for network requests and cookies

chrome.webRequest

STABLE

Use the chrome.webRequest API to observe and analyze traffic and to intercept, block, or modify requests in-flight.

Important

This API requires the "webRequest" permission and host permissions for the URLs you want to intercept.

onBeforeRequest

Fired when a request is about to occur.

EXAMPLE

chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    console.log('Request to:', details.url);
    // Block request
    return {cancel: true};
  },
  {urls: ["*://*.example.com/*"]},
  ["blocking"]
);

onBeforeSendHeaders

Fired before sending an HTTP request.

EXAMPLE

chrome.webRequest.onBeforeSendHeaders.addListener(
  function(details) {
    // Modify headers
    details.requestHeaders.push({
      name: 'Custom-Header',
      value: 'CustomValue'
    });
    return {requestHeaders: details.requestHeaders};
  },
  {urls: ["*://*.example.com/*"]},
  ["blocking", "requestHeaders"]
);

chrome.cookies

STABLE

Use the chrome.cookies API to query and modify cookies, and to be notified when they change.

get()

Retrieves information about a single cookie.

EXAMPLE

chrome.cookies.get({
  url: 'https://example.com',
  name: 'myCookie'
}, function(cookie) {
  console.log('Cookie value:', cookie.value);
});

set()

Sets a cookie with the given cookie data.

EXAMPLE

chrome.cookies.set({
  url: 'https://example.com',
  name: 'myCookie',
  value: 'cookieValue',
  expirationDate: (new Date().getTime() / 1000) + 3600
});

remove()

Deletes a cookie by name.

EXAMPLE

chrome.cookies.remove({
  url: 'https://example.com',
  name: 'myCookie'
});

Content APIs

APIs for content script interactions

chrome.scripting

MANIFEST V3

Use the chrome.scripting API to inject JavaScript and CSS into websites programmatically.

executeScript()

Injects a script into a target context.

EXAMPLE

// Inject script file
chrome.scripting.executeScript({
  target: {tabId: tabId},
  files: ['content-script.js']
});

// Inject inline script
chrome.scripting.executeScript({
  target: {tabId: tabId},
  func: () => {
    document.body.style.backgroundColor = 'red';
  }
});

insertCSS()

Inserts CSS into a target context.

EXAMPLE

chrome.scripting.insertCSS({
  target: {tabId: tabId},
  css: 'body { background-color: #f0f0f0; }'
});

removeCSS()

Removes CSS that was previously inserted.

EXAMPLE

chrome.scripting.removeCSS({
  target: {tabId: tabId},
  css: 'body { background-color: #f0f0f0; }'
});

Common Types

Frequently used type definitions

Tab

{
  id: number,
  index: number,
  windowId: number,
  highlighted: boolean,
  active: boolean,
  pinned: boolean,
  url: string,
  title: string,
  favIconUrl: string,
  status: "loading" | "complete",
  incognito: boolean,
  width: number,
  height: number
}

Window

{
  id: number,
  focused: boolean,
  top: number,
  left: number,
  width: number,
  height: number,
  tabs: Tab[],
  incognito: boolean,
  type: "normal" | "popup" | "panel" | "app" | "devtools",
  state: "normal" | "minimized" | "maximized" | "fullscreen"
}

Need More Help?

Explore additional resources and join our developer community

Ready to Trade?

Start buying or selling extensions today

Featured On

Trusted Platforms