Docs

API

When you create a new Better Auth instance, you get an object called api with a set of functions that you can use to interact with the server.

Any endpoint added on the server will be available on the api object.

Calling API on the Server

Better auth uses a library called better-call to make API endpoints. It's a library made by the same team behind Better Auth and is designed to work seamlessly with it.Better call endpoints allow you to call rest api handlers as if they were normal functions.

server.ts
import { betterAuth } from "better-auth";
 
export const auth = await betterAuth({
    database: {
        provider: "sqlite",
        url: "./db.sqlite",
    },
    plugins: [
        // add your plugins here
    ]
})
 
// calling sign in email on the server
await auth.api.signInEmail({
    body: {
        email: "",
        password: ""
    }
})

Example: Getting the current Session In Next JS

server.ts
import { headers } from "next/server";
 
// calling get session on the server
await auth.api.getSession({
    headers: headers()
})

Unlike the client, the server needs the values to be passed as an object with the key body for the body, headers for the headers, and query for the query.

On this page

Edit on GitHub