- Authentication
- Storage
- GraphQL
- Functions
Import
Usage
Create a client instance to interact with Nhost services:Creating an admin client
You can also create an admin client if needed. This client will have admin access to the database and will bypass permissions. Additionally, it can impersonate users and set any role or session variable. IMPORTANT!!! Keep your admin secret safe and never expose it in client-side code.Interfaces
NhostClient
Main client class that provides unified access to all Nhost services. This class serves as the central interface for interacting with Nhost’s authentication, storage, GraphQL, and serverless functions capabilities.Properties
auth
functions
graphql
sessionStorage
storage
Methods
clearSession()
nhost.auth.signOut({refreshToken: session.refreshTokenId}).
If the middle updateSessionFromResponseMiddleware is used, the session will be removed
from the storage automatically and calling this method is not necessary.
Returns
void
Example
getUserSession()
Returns
null | Session
The current session or null if no session exists
Example
refreshSession()
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
marginSeconds | number | 60 | The number of seconds before the token expiration to refresh the session. If the token is still valid for this duration, it will not be refreshed. Set to 0 to force the refresh. |
Returns
Promise<null | Session>
The new session or null if there is currently no session or if refresh fails
Example
NhostClientOptions
Configuration options for creating an Nhost clientExtended by
Properties
authUrl?
configure?
functionsUrl?
graphqlUrl?
region?
storage?
storageUrl?
subdomain?
NhostServerClientOptions
Configuration options for creating an Nhost clientExtends
Properties
authUrl?
Inherited from
NhostClientOptions.authUrl
configure?
Inherited from
NhostClientOptions.configure
functionsUrl?
Inherited from
NhostClientOptions.functionsUrl
graphqlUrl?
Inherited from
NhostClientOptions.graphqlUrl
region?
Inherited from
NhostClientOptions.region
storage
Overrides
NhostClientOptions.storage
storageUrl?
Inherited from
NhostClientOptions.storageUrl
subdomain?
Inherited from
NhostClientOptions.subdomain
Type Aliases
ClientConfigurationFn()
Parameters
| Parameter | Type |
|---|---|
clients | { auth: Client; functions: Client; graphql: Client; sessionStorage: SessionStorage; storage: Client; } |
clients.auth | Client |
clients.functions | Client |
clients.graphql | Client |
clients.sessionStorage | SessionStorage |
clients.storage | Client |
Returns
void
Variables
withClientSideSessionMiddleware
withServerSideSessionMiddleware
Functions
createClient()
- Instantiating the various service clients (auth, storage, functions and graphql)
- Auto-detecting and configuring an appropriate session storage (localStorage in browsers, memory otherwise)
- Setting up a sophisticated middleware chain for seamless authentication management:
- Automatically refreshing tokens before they expire
- Attaching authorization tokens to all service requests
- Updating the session storage when new tokens are received
Parameters
| Parameter | Type | Description |
|---|---|---|
options | NhostClientOptions | Configuration options for the client |
Returns
NhostClient
A configured Nhost client
Example
createNhostClient()
Parameters
| Parameter | Type | Description |
|---|---|---|
options | NhostClientOptions | Configuration options for the client |
Returns
NhostClient
A configured Nhost client
Example
createServerClient()
- Server components (in frameworks like Next.js or Remix)
- API routes and middleware
- Backend services and server-side rendering contexts
- Requires explicit storage implementation (must be provided)
- Disables automatic session refresh middleware (to prevent race conditions in server contexts)
- Still attaches authorization tokens and updates session storage from responses
Parameters
| Parameter | Type | Description |
|---|---|---|
options | NhostServerClientOptions | Configuration options for the server client (requires storage implementation) |
Returns
NhostClient
A configured Nhost client optimized for server-side usage
Example
generateServiceUrl()
Parameters
| Parameter | Type | Description |
|---|---|---|
serviceType | "auth" | "storage" | "graphql" | "functions" | Type of service (auth, storage, graphql, functions) |
subdomain? | string | Nhost project subdomain |
region? | string | Nhost region |
customUrl? | string | Custom URL override if provided |
Returns
string
The base URL for the service
withAdminSession()
Parameters
| Parameter | Type | Description |
|---|---|---|
adminSession | AdminSessionOptions | Admin session options including admin secret, role, and session variables |
Returns
ClientConfigurationFn
Configuration function that sets up admin middleware