This example shows how to use the E2B Code Interpreter to execute code in Next.js serverless functions.
- Run
npm i - Create a
.envfile in the root of the project and add the following environment variables:
E2B_API_KEY=your_e2b_api_key # Get one at https://e2b.dev/docs
OPENAI_API_KEY=your_openai_api_key- Run
npm run devto start the development server - Open the app in your browser at
http://localhost:3000 - Chat with the app and execute code with the
execute_python_codetool connected to the LLM
Code interpreter implementation is in app/api/chat/codeInterpreter.ts.
The evaluateCode method is the main method that takes Python code to be executed and sessionID. Based on the sessionID it will try to reconnect to an existing sandbox or create a new one if it doesn't exist.
After executing the code it will disconnect from the sandbox and call the .keelAlive method to ensure that the sandbox can be reused for the specified duration.
The code execution is stateful (using Jupyter Notebook underneath) and per session — you can refer to variables from the previous execution, define functions that you will use later, etc.
