npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@letta-ai/letta-nextjs

v0.0.9

Published

Letta's NextJS library

Downloads

615

Readme

Letta NextJS

A Letta-Nextjs library full of functions that should make it easier to build applications on top of Letta and Next.js

Table of Contents

Documentation

Minimal Example

Before you start

letta-nextjs works best with letta-react.

npm install @letta-ai/letta-nextjs @letta-ai/letta-react

Server-side configuration

First create or update your .env file in the root of your project and add the following environment variables:

LETTA_BASE_URL=https://api.letta.com
LETTA_API_KEY=your-api-key-optional

Create or visit your middleware.ts file in root or src directory and add the following code:

import { type NextRequest } from 'next/server';
import { lettaMiddleware } from '@letta-ai/letta-nextjs/server';

export function middleware(request: NextRequest) {
  const response = lettaMiddleware(request, {
    baseUrl: process.env.LETTA_BASE_URL,
    apiKey: process.env.LETTA_API_KEY,
  });

  if (response) {
    return response;
  }
}

export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - api (API routes)
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico, sitemap.xml, robots.txt (metadata files)
     */
    '/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
  ],
};

The code above has been written so you can add additional middleware if you need. If response is null, you can continue with your middleware.

Client Side Usage

Set up the <NextLettaProvider />

Inside your root layout file, import the NextLettaProvider component and wrap it around your application.

import { NextLettaProvider } from '@letta-ai/letta-nextjs/client';

export default function RootLayout({ children }) {
  return <NextLettaProvider>{children}</NextLettaProvider>;
}

Sending, Reading and Interacting with Agents

See the letta-react documentation for more information on how to send, read and interact with agents.

All you would need to do is use the hooks provided in that library.

'use client';
import { useAgentMessages } from '@letta-ai/letta-react';
import { useState } from 'react';

export default function Home() {
  const [message, setMessage] = useState('');
  const { messages, sendMessage } = useAgentMessages({
    agentId: 'agent-id',
  });

  return (
    <div>
      {messages.map((message) => {
        if (message.messageType !== 'assistant_message') {
          return null;
        }

        if (typeof message.content !== 'string') {
          return null;
        }

        return (
          <div key={`${message.id}${message.messageType}`}>
            {message.content}
          </div>
        );
      })}
      <form
        onSubmit={(e) => {
          e.preventDefault();
          sendMessage({
            messages: [{ content: message, role: 'user' }],
          });
          setMessage('');
        }}
      >
        <input value={message} onChange={(e) => setMessage(e.target.value)} />
        <button>Send Message</button>
      </form>
    </div>
  );
}

Plugins

There is a plugin that you can use to extend the functionality of Letta-Nextjs. Right now there is an identityPlugin that you can use to better secure your application.

Identity Plugin

import { type NextRequest } from 'next/server';
import { lettaMiddleware } from '@letta-ai/letta-nextjs/server';
import { identityPlugin } from '@letta-ai/letta-nextjs/plugins';

export async function middleware(request: NextRequest) {
  const response = await lettaMiddleware(request, {
    baseUrl: 'http://localhost:3000',
    apiKey: process.env.LETTA_API_KEY,
    plugins: [
      identityPlugin({
        getIdentity: async (req: NextRequest) => {
          // Replace this with your own identity logic
          // you can return a identity id and Letta will only allow access to the agent if the agent is owned by the identity
          const cookie = req.cookies.get('letta-identity-id');

          return {
            identityId: cookie,
          };
        },
      }),
    ],
  });

  if (response) {
    return response;
  }
}

export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - api (API routes)
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico, sitemap.xml, robots.txt (metadata files)
     */
    '/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
  ],
};