Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ npm install @layercode/node-server-sdk

## Usage

Read the docs: [https://docs.layercode.com/sdk-reference/node_js_sdk](https://docs.layercode.com/sdk-reference/node_js_sdk)
### Next.js authorize helper

```ts
// app/api/authorize/route.ts
export const dynamic = 'force-dynamic';
import { createAuthorizeSessionHandler } from '@layercode/node-server-sdk';

export const POST = createAuthorizeSessionHandler();
```

The helper reads `LAYERCODE_API_KEY` from the environment and proxies the request to Layercode. You can override the key, endpoint, or transform the outgoing body if needed.

For other use cases (webhook streaming, signature verification, etc.) read the docs: [https://docs.layercode.com/sdk-reference/node_js_sdk](https://docs.layercode.com/sdk-reference/node_js_sdk)
77 changes: 77 additions & 0 deletions src/createAuthorizeSessionHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const DEFAULT_AUTHORIZE_ENDPOINT = 'https://api.layercode.com/v1/agents/web/authorize_session';

export interface CreateAuthorizeSessionHandlerOptions {
/**
* Layercode API key. Defaults to process.env.LAYERCODE_API_KEY.
*/
apiKey?: string;
/**
* Override the Layercode authorize session endpoint (mainly for testing).
*/
endpoint?: string;
/**
* Allows you to transform the incoming body before forwarding it to Layercode.
*/
transformBody?: (body: any) => any;
}

/**
* Creates a Next.js-compatible route handler that proxies authorize-session requests to Layercode.
*
* ```ts
* import { createAuthorizeSessionHandler } from 'layercode-node-server-sdk';
*
* export const dynamic = 'force-dynamic';
* export const POST = createAuthorizeSessionHandler();
* ```
*/
export const createAuthorizeSessionHandler = (options: CreateAuthorizeSessionHandlerOptions = {}) => {
const endpoint = options.endpoint ?? DEFAULT_AUTHORIZE_ENDPOINT;

return async function authorizeSessionRoute(request: Request): Promise<Response> {
try {
const apiKey = options.apiKey ?? process.env.LAYERCODE_API_KEY;
if (!apiKey) {
throw new Error('LAYERCODE_API_KEY is not set.');
}

const incomingBody = await request.json();
if (!incomingBody?.agent_id) {
throw new Error('Missing agent_id in request body.');
}

const payload = options.transformBody ? options.transformBody(incomingBody) : incomingBody;

const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify(payload),
});

if (!response.ok) {
const text = await response.text();
throw new Error(text || response.statusText);
}

const json = await response.json();
return new Response(JSON.stringify(json), {
status: 200,
headers: {
'Content-Type': 'application/json',
Comment on lines +60 to +63

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve upstream status and headers when proxying authorize response

The handler always rewraps a successful upstream response into new Response(JSON.stringify(json), { status: 200, headers: { 'Content-Type': 'application/json' } }). This discards the status code and all headers returned by the Layercode endpoint, notably Set-Cookie and any non‑200 status codes. If the authorize endpoint relies on cookies or returns 401/403 for invalid credentials, the caller will never see those headers or the correct status, so the session flow cannot succeed. Consider returning the original response (or cloning it) so status and headers propagate intact.

Useful? React with 👍 / 👎.

},
});
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error';
console.error('Layercode authorize session response error:', message);
return new Response(JSON.stringify({ error: message }), {
status: 500,
headers: {
'Content-Type': 'application/json',
},
});
}
};
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { streamResponse } from './streamResponse';
export { verifySignature } from './verifySignature';
export { createAuthorizeSessionHandler } from './createAuthorizeSessionHandler';
export type { CreateAuthorizeSessionHandlerOptions } from './createAuthorizeSessionHandler';