Skip to main content

getBaseUrl

Get the base URL of the shop, retrieved from configuration or defaulting to localhost.

Import​

import { getBaseUrl } from '@evershop/evershop/lib/util/getBaseUrl';

Syntax​

getBaseUrl(): string

Parameters​

None.

Return Value​

Returns a string containing the base URL without trailing slashes.

Examples​

Basic Usage​

import { getBaseUrl } from '@evershop/evershop/lib/util/getBaseUrl';

const baseUrl = getBaseUrl();
// Returns: 'https://myshop.com' or 'http://localhost:3000'

Building Full URLs​

import { getBaseUrl } from '@evershop/evershop/lib/util/getBaseUrl';

const baseUrl = getBaseUrl();
const productUrl = `${baseUrl}/product/${product.url_key}`;
const categoryUrl = `${baseUrl}/category/${category.url_key}`;

Email Templates​

import { getBaseUrl } from '@evershop/evershop/lib/util/getBaseUrl';

const emailData = {
shopUrl: getBaseUrl(),
orderLink: `${getBaseUrl()}/account/orders/${orderId}`,
logoUrl: `${getBaseUrl()}/assets/logo.png`
};

Resolution order​

The base URL is resolved from the first source that yields a value:

#SourceNotes
1EVERSHOP_HOME_URL environment variableTakes precedence over every config file. Trimmed; an empty value is treated as unset.
2shop.homeUrl configThe usual place to set it for a fixed deployment.
3Localhost on PORT (default 3000)Development fallback.

EVERSHOP_HOME_URL​

EVERSHOP_HOME_URL=https://myshop.com

This is the right knob for container and platform deployments, where the public URL is injected at run time rather than baked into a config file.

A malformed value fails boot

EVERSHOP_HOME_URL is validated at startup. If it is set but is not a parseable absolute URL, or its protocol is not http:/https:, the process throws and exits — every absolute link and email the store generates would otherwise be broken. Leaving it unset (or empty) is fine; the store then falls back to shop.homeUrl.

Config file​

{
"shop": {
"homeUrl": "https://myshop.com"
}
}

Notes​

  • Always removes trailing slashes from the returned URL
  • EVERSHOP_HOME_URL overrides shop.homeUrl, not the other way round
  • Useful for generating absolute URLs in emails, RSS feeds, sitemaps or API responses

See Also​