import{Auth,HTTPException}from"@langchain/langgraph-sdk/auth";// (1) Validate the credentialsconstisValidKey=(key:string)=>{returntrue;};exportconstauth=newAuth().authenticate(async(request:Request)=>{constapiKey=request.headers.get("x-api-key");if(!apiKey||!isValidKey(apiKey)){// (3) Raise an HTTPExceptionthrownewHTTPException(401,{message:"Invalid API key"});}// (2) Return user information containing the user's identity and user information if validreturn{// required, unique user identifieridentity:"user-123",// required, list of permissionspermissions:[],// optional, assumed `true` by defaultis_authenticated:true,// You can add more custom fields if you want to implement other auth patternsrole:"admin",org_id:"org-123",};});
The returned user information is available:
To your authorization handlers via user property in on() callback.
In your application via config["configuration"]["langgraph_auth_user"]
Request input parameter
The authenticate() handler accepts a Request instance as an argument, but the Request object may not include the request body.
You can still use the Request instance to extract other fields such as headers, query parameters etc.
After authentication, LangGraph calls your on() handlers to control access to specific resources (e.g., threads, assistants, crons). These handlers can:
Add metadata to be saved during resource creation by mutating the value["metadata"] object directly. See the supported actions table for the list of types the value can take for each action.
Filter resources by metadata during search/list or read operations by returning a filter object.
Raise an HTTP error if access is denied.
If you want to just implement simple user-scoped access control, you can use a single on() handler for all resources and actions. If you want to have different control depending on the resource and action, you can use resource-specific handlers. See the Supported Resources section for a full list of the resources that support access control.
import{Auth,HTTPException}from"@langchain/langgraph-sdk/auth";exportconstauth=newAuth().authenticate(async(request:Request)=>({identity:"user-123",permissions:[],})).on("*",({value,user})=>{// Create filter to restrict access to just this user's resourcesconstfilters={owner:user.identity};// If the operation supports metadata, add the user identity// as metadata to the resource.if("metadata"invalue){value.metadata??={};value.metadata.owner=user.identity;}// Return filters to restrict access// These filters are applied to ALL operations (create, read, update, search, etc.)// to ensure users can only access their own resourcesreturnfilters;});
You can register handlers for specific resources and actions by chaining the resource and action names together with the on() method.
When a request is made, the most specific handler that matches that resource and action is called. Below is an example of how to register handlers for specific resources and actions. For the following setup:
Authenticated users are able to create threads, read thread, create runs on threads
Only users with the "assistants:create" permission are allowed to create new assistants
All other endpoints (e.g., e.g., delete assistant, crons, store) are disabled for all users.
Supported Handlers
For a full list of supported resources and actions, see the Supported Resources section below.
import{Auth,HTTPException}from"@langchain/langgraph-sdk/auth";exportconstauth=newAuth().authenticate(async(request:Request)=>({identity:"user-123",permissions:["threads:write","threads:read"],})).on("*",({event,user})=>{console.log(`Request for ${event} by ${user.identity}`);thrownewHTTPException(403,{message:"Forbidden"});})// Matches the "threads" resource and all actions - create, read, update, delete, search// Since this is **more specific** than the generic `on("*")` handler, it will take precedence over the generic handler for all actions on the "threads" resource.on("threads",({permissions,value,user})=>{if(!permissions.includes("write")){thrownewHTTPException(403,{message:"User lacks the required permissions.",});}// Not all events do include `metadata` property in `value`.// So we need to add this type guard.if("metadata"invalue){value.metadata??={};value.metadata.owner=user.identity;}return{owner:user.identity};})// Thread creation. This will match only on thread create actions.// Since this is **more specific** than both the generic `on("*")` handler and the `on("threads")` handler, it will take precedence for any "create" actions on the "threads" resources.on("threads:create",({value,user,permissions})=>{if(!permissions.includes("write")){thrownewHTTPException(403,{message:"User lacks the required permissions.",});}// Setting metadata on the thread being created will ensure that the resource contains an "owner" field// Then any time a user tries to access this thread or runs within the thread,// we can filter by ownervalue.metadata??={};value.metadata.owner=user.identity;return{owner:user.identity};})// Reading a thread. Since this is also more specific than the generic `on("*")` handler, and the `on("threads")` handler,.on("threads:read",({user})=>{// Since we are reading (and not creating) a thread,// we don't need to set metadata. We just need to// return a filter to ensure users can only see their own threads.return{owner:user.identity};})// Run creation, streaming, updates, etc.// This takes precedence over the generic `on("*")` handler and the `on("threads")` handler.on("threads:create_run",({value,user})=>{value.metadata??={};value.metadata.owner=user.identity;return{owner:user.identity};})// Assistant creation. This will match only on assistant create actions.// Since this is **more specific** than both the generic `on("*")` handler and the `on("assistants")` handler, it will take precedence for any "create" actions on the "assistants" resources.on("assistants:create",({value,user,permissions})=>{if(!permissions.includes("assistants:create")){thrownewHTTPException(403,{message:"User lacks the required permissions.",});}// Setting metadata on the assistant being created will ensure that the resource contains an "owner" field.// Then any time a user tries to access this assistant, we can filter by ownervalue.metadata??={};value.metadata.owner=user.identity;return{owner:user.identity};});
Notice that we are mixing global and resource-specific handlers in the above example. Since each request is handled by the most specific handler, a request to create a thread would match the thread:create handler but NOT the * handler. A request to update a thread, however would be handled by the global handler, since we don't have a more specific handler for that resource and action. Requests to create, update,
Authorization handlers can return None, a boolean, or a filter object.
null, void and true mean "authorize access to all underling resources"
False means "deny access to all underling resources (raises a 403 error)"
A metadata filter object will restrict access to resources. Supports exact matches and operators.
Filter object syntax
The following operators are supported:
Exact match shorthand: {"field": "value"}
Exact match: {"field": {"$eq": "value"}}
Contains: {"field": {"$contains": "value"}}
A metadata filter object with multiple keys is treated using a logical AND filter. For example, {"owner": org_id, "allowed_users": {"$contains": user_id}} will only match resources with metadata whose "owner" is org_id and whose "allowed_users" list contains user_id.
This common pattern lets you scope all threads, assistants, crons, and runs to a single user. It's useful for common single-user use cases like regular chatbot-style apps.
This pattern lets you control access based on permissions. It's useful if you want certain roles to have broader or more restricted access to resources.
import{Auth,HTTPException}from"@langchain/langgraph-sdk/auth";exportconstauth=newAuth().authenticate(async(request:Request)=>({identity:"user-123",// Define permissions in authpermissions:["threads:write","threads:read"],})).on("threads:create",({value,user,permissions})=>{if(!permissions.includes("threads:write")){thrownewHTTPException(403,{message:"Unauthorized"});}if("metadata"invalue){value.metadata??={};value.metadata.owner=user.identity;}return{owner:user.identity};}).on("threads:read",({user,permissions})=>{if(!permissions.includes("threads:read")&&!permissions.includes("threads:write")){thrownewHTTPException(403,{message:"Unauthorized"});}return{owner:user.identity};});
LangGraph provides three levels of authorization handlers, from most general to most specific:
Global Handler (on("*")): Matches all resources and actions
Resource Handler (e.g., on("threads"), on("assistants"), on("crons")): Matches all actions for a specific resource
Action Handler (e.g., on("threads:create"), on("threads:read")): Matches a specific action on a specific resource
The most specific matching handler will be used. For example, on("threads:create") takes precedence over on("threads") for thread creation. If a more specific handler is registered, the more general handler will not be called for that resource and action.
Runs are scoped to their parent thread for access control. This means permissions are typically inherited from the thread, reflecting the conversational nature of the data model. All run operations (reading, listing) except creation are controlled by the thread's handlers.
There is a specific threads:create_run event for creating new runs because it had more arguments that you can view in the handler.