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

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/getting_started/installation/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, instead of a long-running process in Node.js.
- Some of LlamaIndex.TS packages are not available in Cloudflare Worker, for example `@llamaindex/readers` and `@llamaindex/huggingface`.
- The main `llamaindex` 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.


