Skip to content

Introduction

cli-aware detects whether an HTTP request comes from a CLI tool (curl, wget, httpie, …), a browser, or something else, allowing you to consistently serve different content based on the environment.

npm version npm downloads

Terminal window
npm install cli-aware
import CliAware from 'cli-aware';
// Works with any Request-like object
const aware = new CliAware(request);
if (aware.cli) {
// The request comes from curl, wget, httpie, etc.
console.log(`CLI client: ${aware.client}`); // e.g. "curl"
}
if (aware.browser) {
// The request comes from Chrome, Firefox, Safari, etc.
}
if (aware.unknown) {
// Neither a known CLI client nor a recognized browser
}
+server.ts
import CliAware from 'cli-aware';
import type { RequestHandler } from './$types';
export const GET: RequestHandler = ({ request }) => {
const aware = new CliAware(request);
if (aware.cli) {
return new Response(`Hello, ${aware.client} user!`);
}
return new Response('Hello, browser user!');
};

If you only need a boolean check:

import { isCliRequest } from 'cli-aware';
if (isCliRequest(request)) {
// handle CLI
}
Property Type Description
request R (the input you passed) The original request object
options O (the options you passed) The options object (defaults to {})
cli boolean true if the request comes from a known CLI client
browser boolean true if the request comes from a recognized browser
unknown boolean true if the client is neither a known CLI tool nor a browser
client string | null The matched client token (e.g. "curl") when cli is true, otherwise null

Exactly one of cli, browser, or unknown is always true.

CliAware accepts any object matching Request | { headers?: RequestHeaders }:

  • Fetch API Request — from Deno, Bun, Cloudflare Workers, SvelteKit, etc.
  • Node.js IncomingMessage — from Express, Koa, Fastify, etc. (plain { headers: Record<string, string> })
  • Any object with a headers property — Headers instance or plain object

Header lookup is always case-insensitive.

Pull requests are welcome! Before opening a PR, please run:

Terminal window
npm run check && npm run format