Registry / api / wpcom-xhr-request

wpcom-xhr-request

JSON →
library1.3.1jsnpmunverified

wpcom-xhr-request is a lightweight browser library for making authenticated REST API requests to WordPress.com using XMLHttpRequest and CORS. Version 1.3.1 is the latest stable release. It handles OAuth 1.0a authentication and token management, providing a simpler interface than raw XMLHttpRequest or fetch with custom auth headers. Unlike the Node.js counterpart wpcom.js, this package is specifically designed for client-side use in browsers. It has low release cadence, with occasional maintenance updates. Key differentiators: automatic OAuth signing, cookie-based token storage, and integration with WordPress.com's API endpoints.

npm install wpcom-xhr-request
INSTALL
IMPORT
SIG · WPCOM-XHR-REQUEST
W
wpcom-xhr-request
apijavascriptv1.3.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import wpcomXhrRequest from 'wpcom-xhr-request'
const wpcomXhrRequest = require('wpcom-xhr-request')
The package is ESM-only. CommonJS require will fail.
WPCOMXhrRequest
import WPCOMXhrRequest from 'wpcom-xhr-request'
import { WPCOMXhrRequest } from 'wpcom-xhr-request'
This is the default export, not a named export. Use default import.
getRequestToken
import { getRequestToken } from 'wpcom-xhr-request'
import getRequestToken from 'wpcom-xhr-request'
getRequestToken is a named export; must be destructured.

Shows basic authenticated GET request to the WordPress.com API /me endpoint with callback handling.

import wpcomXhrRequest from 'wpcom-xhr-request'; wpcomXhrRequest( { path: '/me', method: 'GET', apiVersion: '1.1', accessToken: process.env.ACCESS_TOKEN ?? 'your-token-here' }, ( error, body, headers ) => { if ( error ) { console.error( error ); return; } console.log( body ); } );
Debug
Known issues
deprecatedXMLHttpRequest is deprecated in modern browsers in favor of fetch API.
fix
Consider using wpcom-fetch or native fetch with a library like wpcom-oauth-utils.
affects: >=1.0
gotchaOAuth authentication requires a valid access token; token management is not handled.
fix
Obtain an access token via the WordPress.com OAuth flow before making requests.
affects: >=1.0
breakingDefault export changed from function to object in version 1.2.0.
fix
If using older versions, update imports: `import wpcomXhrRequest from 'wpcom-xhr-request'` now works directly.
affects: >=1.2.0
gotchaCORS errors may occur if not using correct credentials or if the request is from a non-allowed origin.
fix
Ensure the WordPress.com application is configured with the correct allowed origins.
affects: >=1.0
deprecatedThe package does not support the WordPress.com API v2 endpoints out of the box.
fix
Use the apiVersion parameter to specify the version, or switch to a newer library.
affects: >=1.0
Errors
Common errors & fixes
TypeError: wpcomXhrRequest is not a function
Importing the default export incorrectly, e.g., using named import syntax.
fix
Use `import wpcomXhrRequest from 'wpcom-xhr-request'` instead of `import { wpcomXhrRequest } from 'wpcom-xhr-request'`.
XMLHttpRequest cannot load https://public-api.wordpress.com/rest/v1.1/me. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.
CORS preflight fails because the origin is not whitelisted in the WordPress.com app settings.
fix
Add your app's origin to the 'Allowed origins' list in WordPress.com Developer Console.
Error: No access token provided
The accessToken parameter was omitted or undefined.
fix
Pass a valid access token via the accessToken option in the request object.
Upgrade
Version history
1.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
34 hits · last 30 days
node
30
OpenAI (training)
1
Resources
wpcom-xhr-request — npm install wpcom-xhr-request · libregistry