---
title: With Cloudflare Worker
description: In this guide, you'll learn how to use LlamaIndex with CloudFlare Worker
---

import {
  SiNodedotjs,
  SiDeno,
  SiBun,
  SiCloudflareworkers,
} from "@icons-pack/react-simple-icons";

Before you start, make sure you have try LlamaIndex.TS in Node.js to make sure you understand the basics.

<Card
  title="Getting Started with LlamaIndex.TS in Node.js"
  href="/docs/llamaindex/setup/node"
/>

Also, you need have the basic understanding of <a href='https://developers.cloudflare.com/workers/'><SiCloudflareworkers className="inline mr-2" color="#F38020" />Cloudflare Worker</a>.

## Adding environment variables

```ts
export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const { setEnvs } = await import("@llamaindex/env");
    setEnvs(env);
    const { OpenAIAgent } = await import("@llamaindex/openai");
    // Start your code here
    return new Response("Hello, world!");
  },
};
```

Then, you need create `.dev.vars` and add LLM api keys for the local development, such as `OPENAI_API_KEY` for OpenAI API key.

<Callout type="warn">Do not commit the api key to git repository.</Callout>

## Integrating with Hono

```ts
import { Hono } from "hono";

type Bindings = {
  OPENAI_API_KEY: string;
};

const app = new Hono<{
  Bindings: Bindings;
}>();

app.post("/llm", async (c) => {
  const { setEnvs } = await import("@llamaindex/env");
  setEnvs(c.env);

  // ...

  return new Response('Hello, world!');
})

export default {
  fetch: app.fetch,
};
```

## Difference between Node.js and Cloudflare Worker

In Cloudflare Worker and similar serverless JS environment, you need to be aware of the following differences:

- Some Node.js modules are not available in Cloudflare Worker, such as `node:fs`, `node:child_process`, `node:cluster`...
- You are recommend to design your code using network request, such as use `fetch` API to communicate with database, insteadof a long-running process in Node.js.
- Some of LlamaIndex.TS modules are not available in Cloudflare Worker, for example `SimpleDirectoryReader` (requires `node:fs`), Some multimodal API that relies on [`onnxruntime-node`](https://www.npmjs.com/package/onnxruntime-node)(we might port to HTTP based module in the future).
- `@llamaindex/core` is designed to work in all JavaScript environment, including Cloudflare Worker. If you find any issue, please report to us.
- `@llamaindex/env` is a JS environment binding module, which polyfill some Node.js/Modern Web API (for example, we have a memory based `fs` module, and Crypto API polyfill). It is designed to work in all JavaScript environment, including Cloudflare Worker.

## Known issues

- `llamaindex` not work perfectly in Cloudflare Worker, bundle size will be larger than 1MB, which is the limit of Cloudflare Worker. You will need import submodule instead of the whole `llamaindex` module.
