Skip to main content

JavaScript

The Nhost JavaScript client is the primary way of interacting with your Nhost project. It exposes a standard interface for each of the following services:

Installation

Install the Nhost client together with GraphQL:

npm install @nhost/nhost-js graphql

Initializing

Initialize a single nhost instance using your Nhost subdomain and region:

src/lib/nhost.ts
import { NhostClient } from '@nhost/nhost-js'

export const nhost = new NhostClient({
subdomain: '<your-subdomain>',
region: '<your-region>'
})

Using custom URLs

There are cases where you might want to use a custom URL for one or more of the services (e.g: when you are self-hosting or you are running services on custom ports). You can do this by passing in the custom URLs when initializing the Nhost client:

src/lib/nhost.ts
import { NhostClient } from '@nhost/nhost-js'

export const nhost = new NhostClient({
authUrl: 'https://auth.mydomain.com/v1',
storageUrl: 'https://storage.mydomain.com/v1',
graphqlUrl: 'https://graphql.mydomain.com/v1',
functionsUrl: 'https://functions.mydomain.com/v1'
})

GraphQL Support

The Nhost client has a small GraphQL client built-in which is great to use server-side or in very simple frontend apps. For more serious frontend apps, we recommend using a more complete GraphQL client such as:

Tokens and Permissions

The Nhost client manages refresh tokens and access tokens automatically. The correct authorization headers are set if the user is signed in. Both access tokens and refresh tokens are automatically refreshed in the background.

If the user is not signed in no authorization header is set. If no authorization header is set, requests to GraphQL and Storage resolve permissions using the public role.

TypeScript Support

The Nhost JavaScript client is written in TypeScript and has full TypeScript support.