Skip to main content
Stack Auth provides specialized app classes for Next.js server and client environments.

StackServerApp

The StackServerApp class provides server-side authentication with access to user management APIs.

Constructor Options

tokenStore
string
required
Token storage mechanism. Use "nextjs-cookie" for Next.js applications.
projectId
string
Your Stack Auth project ID. Defaults to NEXT_PUBLIC_STACK_PROJECT_ID environment variable.
publishableClientKey
string
Your publishable client key. Defaults to NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY.
secretServerKey
string
Your secret server key. Defaults to STACK_SECRET_SERVER_KEY environment variable.
urls
object
Custom URL configuration for authentication pages.

Example

// stack.ts
import "server-only";
import { StackServerApp } from "@stackframe/stack";

export const stackServerApp = new StackServerApp({
  tokenStore: "nextjs-cookie",
  urls: {
    accountSettings: '/settings',
    afterSignIn: '/dashboard',
    afterSignUp: '/onboarding',
  }
});

Core Methods

getUser()

Retrieve the current authenticated user on the server.
// app/page.tsx
import { stackServerApp } from './stack';

export default async function Page() {
  const user = await stackServerApp.getUser();
  
  if (!user) {
    return <div>Not signed in</div>;
  }
  
  return <div>Welcome, {user.displayName}!</div>;
}
options
object
Returns: Promise<CurrentServerUser | null>

createUser()

Create a new user programmatically.
const newUser = await stackServerApp.createUser({
  primaryEmail: '[email protected]',
  password: 'secure_password',
  displayName: 'John Doe',
});
options
object
required
Returns: Promise<ServerUser>

listUsers()

List all users in the project.
const users = await stackServerApp.listUsers({
  limit: 50,
  orderBy: 'createdAt',
});

console.log(`Found ${users.length} users`);
console.log(`Next cursor: ${users.nextCursor}`);
options
object
Returns: Promise<ServerUser[] & { nextCursor: string | null }>

createTeam()

Create a new team.
const team = await stackServerApp.createTeam({
  displayName: 'Engineering Team',
});
data
object
required
Returns: Promise<ServerTeam>

sendEmail()

Send a custom email using Stack Auth’s email service.
await stackServerApp.sendEmail({
  to: '[email protected]',
  subject: 'Welcome!',
  html: '<p>Welcome to our app!</p>',
});
options
object
required
Returns: Promise<void>

StackClientApp

For client-side usage in React components, see the React SDK documentation.

StackAdminApp

The StackAdminApp extends StackServerApp with full administrative capabilities.

Constructor Options

Includes all StackServerApp options plus:
superSecretAdminKey
string
Your super secret admin key. Defaults to STACK_SUPER_SECRET_ADMIN_KEY environment variable.

Example

import "server-only";
import { StackAdminApp } from "@stackframe/stack";

export const stackAdminApp = new StackAdminApp({
  tokenStore: "nextjs-cookie",
  superSecretAdminKey: process.env.STACK_SUPER_SECRET_ADMIN_KEY,
});

Admin Methods

Additional methods available on StackAdminApp:
  • createInternalApiKey() - Create API keys
  • createTeamPermissionDefinition() - Define team permissions
  • createProjectPermissionDefinition() - Define project permissions
  • sendTestEmail() - Send test emails
  • listSentEmails() - View email history
  • queryAnalytics() - Query analytics data
See the API Reference for complete documentation.

Build docs developers (and LLMs) love