Express

Learn how to use the AI TOOLKIT in an Express server

2 min readapi serversstreamingView source

You can use the AI TOOLKIT in an Express server to generate and stream text and objects to the client.

Run it locally

$ npm install ai

Examples

The examples start a simple HTTP server that listens on port 8080. You can e.g. test it using curl:

curl -X POST http://localhost:8080

<Note>

The examples use the Vercel AI Gateway. Ensure that your AI Gateway API key is

set in the AI_GATEWAY_API_KEY environment variable.

</Note>

Full example: github.com/khulnasoft/ai-toolkit/examples/express

### UI Message Stream

You can use the pipeUIMessageStreamToResponse method to pipe the stream data to the server response.

import { streamText } from 'ai-toolkit';

import express, { Request, Response } from 'express';

const app = express();

app.post('/', async (req: Request, res: Response) => {

const result = streamText({

model: 'openai/gpt-4o',

prompt: 'Invent a new holiday and describe its traditions.',

});

result.pipeUIMessageStreamToResponse(res);

});

app.listen(8080, () => {

console.log(Example app listening on port ${8080});

});

### Sending Custom Data

pipeUIMessageStreamToResponse can be used to send custom data to the client.

import {

createUIMessageStream,

pipeUIMessageStreamToResponse,

streamText,

} from 'ai-toolkit';

import express, { Request, Response } from 'express';

const app = express();

app.post('/custom-data-parts', async (req: Request, res: Response) => {

pipeUIMessageStreamToResponse({

response: res,

stream: createUIMessageStream({

execute: async ({ writer }) => {

writer.write({ type: 'start' });

writer.write({

type: 'data-custom',

data: {

custom: 'Hello, world!',

},

});

const result = streamText({

model: 'openai/gpt-4o',

prompt: 'Invent a new holiday and describe its traditions.',

});

writer.merge(result.toUIMessageStream({ sendStart: false }));

},

}),

});

});

app.listen(8080, () => {

console.log(Example app listening on port ${8080});

});

### Text Stream

You can send a text stream to the client using pipeTextStreamToResponse.

import { streamText } from 'ai-toolkit';

import express, { Request, Response } from 'express';

const app = express();

app.post('/', async (req: Request, res: Response) => {

const result = streamText({

model: 'openai/gpt-4o',

prompt: 'Invent a new holiday and describe its traditions.',

});

result.pipeTextStreamToResponse(res);

});

app.listen(8080, () => {

console.log(Example app listening on port ${8080});

});

bash
  1. 1curl -X POST http://localhost:8080
index.ts
ts
  1. 1import { streamText } from 'ai-toolkit';
  2. 2import express, { Request, Response } from 'express';
  3. 3const app = express();
  4. 4app.post('/', async (req: Request, res: Response) => {
  5. 5 const result = streamText({
  6. 6 model: 'openai/gpt-4o',
  7. 7 prompt: 'Invent a new holiday and describe its traditions.',
  8. 8 });
  9. 9 result.pipeUIMessageStreamToResponse(res);
  10. 10});
  11. 11app.listen(8080, () => {
  12. 12 console.log(`Example app listening on port ${8080}`);
  13. 13});
index.ts
ts
  1. 1import {
  2. 2 createUIMessageStream,
  3. 3 pipeUIMessageStreamToResponse,
  4. 4 streamText,
  5. 5} from 'ai-toolkit';
  6. 6import express, { Request, Response } from 'express';
  7. 7const app = express();
  8. 8app.post('/custom-data-parts', async (req: Request, res: Response) => {
  9. 9 pipeUIMessageStreamToResponse({
  10. 10 response: res,
  11. 11 stream: createUIMessageStream({
  12. 12 execute: async ({ writer }) => {
  13. 13 writer.write({ type: 'start' });
  14. 14 writer.write({
  15. 15 type: 'data-custom',
  16. 16 data: {
  17. 17 custom: 'Hello, world!',
  18. 18 },
  19. 19 });
  20. 20 const result = streamText({
  21. 21 model: 'openai/gpt-4o',
  22. 22 prompt: 'Invent a new holiday and describe its traditions.',
  23. 23 });
  24. 24 writer.merge(result.toUIMessageStream({ sendStart: false }));
  25. 25 },
  26. 26 }),
  27. 27 });
  28. 28});
  29. 29app.listen(8080, () => {
  30. 30 console.log(`Example app listening on port ${8080}`);
  31. 31});
index.ts
ts
  1. 1import { streamText } from 'ai-toolkit';
  2. 2import express, { Request, Response } from 'express';
  3. 3const app = express();
  4. 4app.post('/', async (req: Request, res: Response) => {
  5. 5 const result = streamText({
  6. 6 model: 'openai/gpt-4o',
  7. 7 prompt: 'Invent a new holiday and describe its traditions.',
  8. 8 });
  9. 9 result.pipeTextStreamToResponse(res);
  10. 10});
  11. 11app.listen(8080, () => {
  12. 12 console.log(`Example app listening on port ${8080}`);
  13. 13});

Troubleshooting

- Streaming not working when proxied