SharePoint Unattended HTTP Authentication
raw JSON →node-sp-auth is a JavaScript/TypeScript library designed for performing unattended (non-interactive) HTTP authentication against SharePoint environments, supporting SharePoint 2013 and newer, as well as SharePoint Online. It automatically determines the appropriate authentication strategy (e.g., NTLM, FBA, SAML, ADFS, Add-in only) based on provided credentials and handles credential caching for performance. The current stable version is 3.0.9. However, the package is no longer actively maintained, with no new features or versions expected. Developers are strongly advised to migrate to `@azure/msal-node` for modern SharePoint Online authentication, especially as some authentication methods, particularly those relying on the SharePoint Add-in model, will cease to function after April 2026 due to Microsoft's retirement of Azure ACS and the Add-in model.
Common errors
error Access denied. You do not have permission to perform this action or access this resource. ↓
@azure/msal-node, such as client credentials or device code flow, for SharePoint Online. error Error: NTLM authentication failed: unable to negotiate ↓
request-promise or similar, ensure the agent property from authResult.options.agent is included in your request configuration object. error Could not authenticate to SharePoint. Invalid credentials provided. ↓
credentialOptions object structure and values against the node-sp-auth Wiki for the specific authentication strategy (e.g., SAML, FBA, NTLM) you are attempting to use. Verify usernames, passwords, client IDs, and secrets. Warnings
deprecated The `node-sp-auth` package is no longer actively maintained by its author. No new features or versions are expected. Development is primarily limited to community-contributed PRs. ↓
breaking Some authentication methods, particularly those relying on the SharePoint Add-in model and Azure ACS, will cease to function after April 2026 due to Microsoft's retirement of these services. This will directly impact `node-sp-auth` users relying on these specific flows. ↓
gotcha For SharePoint on-premise NTLM authentication, the `getAuth` method might return an `agent` property within the `options` object (i.e., `authResult.options.agent`). This custom HTTP agent *must* be passed to the underlying HTTP client for successful authentication; otherwise, requests will fail. ↓
Install
npm install node-sp-auth yarn add node-sp-auth pnpm add node-sp-auth Imports
- spauth wrong
import spauth from 'node-sp-auth';correctimport * as spauth from 'node-sp-auth'; - getAuth wrong
import { getAuth } from 'node-sp-auth';correctimport * as spauth from 'node-sp-auth'; const authOptions = await spauth.getAuth(url, credentials); - spauth (CommonJS) wrong
const { getAuth } = require('node-sp-auth');correctconst spauth = require('node-sp-auth');
Quickstart
import * as spauth from 'node-sp-auth';
import * as request from 'request-promise'; // Ensure request-promise is installed
async function authenticateAndFetch() {
const url = process.env.SP_SITE_URL ?? 'https://yourtenant.sharepoint.com/sites/dev';
const username = process.env.SP_USERNAME ?? '';
const password = process.env.SP_PASSWORD ?? '';
if (!username || !password) {
console.warn("SP_USERNAME or SP_PASSWORD environment variables are not set. This example might fail without proper credentials.");
}
const credentialOptions = {
username: username,
password: password
// Add other credential options (e.g., tenant ID, client ID, client secret) as needed
// for specific authentication types like SAML or ADFS.
};
try {
const authResult = await spauth.getAuth(url, credentialOptions);
const headers = authResult.headers;
headers['Accept'] = 'application/json;odata=verbose';
console.log('Authentication successful. Making request...');
const response = await request.get({
url: `${url}/_api/web`,
headers: headers,
// IMPORTANT: For NTLM authentication, pass the 'agent' property if it exists
agent: authResult.options?.agent
});
console.log('Successfully fetched web data:');
console.log(JSON.parse(response));
} catch (error) {
console.error('Authentication or request failed:', error);
}
}
authenticateAndFetch();