|
1 | | -This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app). |
| 1 | +# OpenRouter SDK Next.js Demo |
2 | 2 |
|
3 | | -## Getting Started |
| 3 | +This is a demonstration application that shows how to create a chat interface |
| 4 | +using the [OpenRouter SDK](https://github.com/openrouterteam/typescript-sdk). The |
| 5 | +app implements OAuth 2.0 authentication flow and provides a complete chat |
| 6 | +experience with multiple AI models. |
4 | 7 |
|
5 | | -First, run the development server: |
| 8 | +> **⚠️ Security Notice**: This demo application stores API keys in localStorage |
| 9 | +> for demonstration purposes only. **Do not use this approach in production |
| 10 | +> applications**. |
6 | 11 |
|
| 12 | +## Features |
| 13 | + |
| 14 | +- **OAuth 2.0 Integration**: Secure authentication flow with OpenRouter |
| 15 | +- **Multi-Model Chat**: Access to various AI models (GPT-4, Claude, Gemini, etc.) |
| 16 | +- **Real-time Streaming**: Live response streaming for better user experience |
| 17 | +- **Modern UI**: Clean interface built with Tailwind CSS and shadcn/ui components |
| 18 | +- **TypeScript**: Full type safety with the OpenRouter SDK |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +- Node.js 18 or later |
| 23 | +- An [OpenRouter account](https://openrouter.ai) |
| 24 | +- OpenRouter application configured with OAuth callback |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +1. Clone the repository and navigate to the example directory: |
| 29 | +```bash |
| 30 | +git clone <repository-url> |
| 31 | +cd typescript-sdk/examples/nextjs-example |
| 32 | +``` |
| 33 | + |
| 34 | +2. Install dependencies: |
| 35 | +```bash |
| 36 | +npm install |
| 37 | +``` |
| 38 | + |
| 39 | +3. Start the development server: |
7 | 40 | ```bash |
8 | 41 | npm run dev |
9 | | -# or |
10 | | -yarn dev |
11 | | -# or |
12 | | -pnpm dev |
13 | | -# or |
14 | | -bun dev |
15 | 42 | ``` |
16 | 43 |
|
17 | | -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. |
| 44 | +4. Open [http://localhost:3000](http://localhost:3000) in your browser |
| 45 | + |
| 46 | +## Configuration |
| 47 | + |
| 48 | +### OAuth Setup |
| 49 | + |
| 50 | +The application is pre-configured to use `http://localhost:3000/` as the OAuth |
| 51 | +callback URL. If you need to change this: |
| 52 | + |
| 53 | +1. Update the `OAUTH_CALLBACK_URL` in `src/lib/config.ts` |
| 54 | +2. Configure your OpenRouter application with the matching callback URL |
| 55 | + |
| 56 | +### Environment Variables |
| 57 | + |
| 58 | +No environment variables are required for this demo, as it uses client-side |
| 59 | +OAuth flow. |
| 60 | + |
| 61 | +## Usage |
| 62 | + |
| 63 | +1. **Connect Your Account**: Click "Connect OpenRouter Account" to authenticate |
| 64 | + via OAuth |
| 65 | +2. **Start Chatting**: Once connected, navigate to the chat interface |
| 66 | +3. **Select Models**: Choose from available AI models using the dropdown menu |
| 67 | +4. **Send Messages**: Type your message and press Enter to send |
| 68 | + |
| 69 | +## Project Structure |
| 70 | + |
| 71 | +``` |
| 72 | +src/ |
| 73 | +├── app/ |
| 74 | +│ ├── page.tsx # Home page with OAuth flow |
| 75 | +│ ├── chat/page.tsx # Chat interface |
| 76 | +│ └── oauth/callback/ # OAuth callback handler |
| 77 | +├── components/ui/ # Reusable UI components |
| 78 | +├── lib/ |
| 79 | +│ ├── config.ts # OAuth and app configuration |
| 80 | +│ └── hooks/ # Custom React hooks |
| 81 | +└── ... |
| 82 | +``` |
| 83 | + |
| 84 | +## Key Implementation Details |
| 85 | + |
| 86 | +### OAuth Flow |
18 | 87 |
|
19 | | -You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. |
| 88 | +- Uses OpenRouter's OAuth 2.0 implementation |
| 89 | +- Handles authorization codes and token exchange |
| 90 | +- Stores credentials in localStorage (demo only) |
20 | 91 |
|
21 | | -This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel. |
| 92 | +### Chat Integration |
22 | 93 |
|
23 | | -## Learn More |
| 94 | +- Implements streaming responses using the OpenRouter SDK |
| 95 | +- Supports multiple AI models through a single interface |
| 96 | +- Real-time message updates with proper state management |
24 | 97 |
|
25 | | -To learn more about Next.js, take a look at the following resources: |
| 98 | +### SDK Usage |
26 | 99 |
|
27 | | -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. |
28 | | -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. |
| 100 | +```typescript |
| 101 | +import { OpenRouterCore } from "@openrouter/sdk/core"; |
| 102 | +import { chatSend } from "@openrouter/sdk/funcs/chatSend"; |
29 | 103 |
|
30 | | -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome! |
| 104 | +const openRouter = new OpenRouterCore({ apiKey }); |
| 105 | +const result = await chatSend(openRouter, { |
| 106 | + model: "openai/gpt-4o", |
| 107 | + messages: conversationHistory, |
| 108 | + stream: true |
| 109 | +}); |
| 110 | +``` |
31 | 111 |
|
32 | | -## Deploy on Vercel |
| 112 | +## Development Commands |
33 | 113 |
|
34 | | -The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. |
| 114 | +- `npm run dev` - Start development server |
| 115 | +- `npm run build` - Build for production |
| 116 | +- `npm run start` - Start production server |
| 117 | +- `npm run lint` - Run ESLint |
35 | 118 |
|
36 | | -Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details. |
|
0 commit comments