Skip to content

Function: basicAuth() ​

basicAuth(user, pass): BeforeRequestHook

Defined in: src/auth.ts:113

Returns a beforeRequest hook that adds an Authorization: Basic <credentials> header before every request. Credentials are encoded once at creation time.

The user:pass string is UTF-8 encoded before base64 (not btoa directly, which only supports Latin-1), so non-ASCII usernames/passwords are safe.

Parameters ​

user ​

string

The username (must not contain a colon).

pass ​

string

The password.

Returns ​

BeforeRequestHook

A beforeRequest hook that sets the Authorization header.

Example ​

ts
import valifetch from 'valifetch';
import { basicAuth } from 'valifetch/auth';

const api = valifetch.create({
  hooks: { beforeRequest: [basicAuth('admin', 's3cr3t')] },
});

Released under the MIT License. Built with Claude Code.