Video: Secure Vibe Coding and Sandboxing for Developers | Duration: 4488s | Summary: Secure Vibe Coding and Sandboxing for Developers | Chapters: Welcome and Introduction (1.2s), AI Coding Tools (86.33s), Interactive Slide Presentation (280.99s), Sandbox SDK Goals (365.385s), Sandbox SDK Security (446.025s), Project Setup Instructions (997.73s), Configuring AI Bindings (1448.6s), Understanding Sandbox Code (1622.325s), Exercise and Resources (1951.705s), Code Execution Demo (2220.1s), Executing AI-Generated Code (3142.2s), Production Security Practices (3512.42s), Conclusion and Resources (4171s), Conclusion and Farewell (4306.695s)
Transcript for "Secure Vibe Coding and Sandboxing for Developers": Well, hello, everyone. Welcome to today's session, building a secure AI code execution on the edge with CloudFare sandbox SDK. Now I am really excited for today because this is one of the Cloudflare products that I personally use a lot, and I have been building a lot on this. So before we get into the session, I am gonna wait for a couple of minutes for everyone to join in. In the meantime, what I would love from you all to know is where are you joining in from? I have a bit of an idea, like, which region this session is getting held. I know in it's in the APAC region, it's kind of morning ish afternoon, but we'd love to know where are folks joining in from. So do let us know in the chat where are you joining in from. Oh, we got someone from Croatia, Malaysia, Singapore. Awesome. It's super early morning for Croatia. I really appreciate it. I am based in Berlin in Germany, and it's quite a bit early for me to do this session. So I I am excited for folks joining in from Europe. Thank you for joining in. I see India. I see Philippines. I saw New Zealand. I see Bangkok, Thailand. Wow. Okay. There is, like we got a very amazing audience in here that seems like it. Now I am gonna start with sharing my screen so that I can start with the presentation. So let me bring up my screen over here. Alright. So I am excited over here, as I mentioned earlier, and I also have a few questions for you all before I jump into the slides. The very first question I have is gonna be on the poll. So when we click on the poll, you will see a poll tab right next to the chat tab where you can give me an answer. So the poll so the first question I have for you all is, do you use AI tools to write code? Now this can be you using just plain old ChatGPT or Gemini or Clod to ask questions, get the code, and paste it in your project. Or, like, go full into using tools like OpenCode, Cloud Code, Gemini, I see, like, Codex, whatever. Then curious to know from folks what kind of tools you use and if you even use those tools. And, again, the way this is gonna work is you select the options from the push tab, and we will be then seeing that result on the screen when I share that. I'm gonna give folks a couple of seconds over here to share their replies. I see someone mentioned that they are using OpenCode y r t y, which is fantastic. We have I I have been using OpenCode a lot lately. I was a Claude code user for a long time, and now I have been using OpenCode. Alright. I see that words coming in. So I'm gonna close that and see I see, like, a lot of people are using this tools as well. So it's ClotCode, ClotCode, OpenCode. That's fantastic. Alright. And we I am gonna move on to the next part is because you all mentioned you have been kind of, you know, using these tools. Have you tried wipe coding? And when I mean wipe coding, it's like one shot. Not one shot, but let's say you just, you know, went in, asked you to build the whole app for you. Have you tried that for yourself? Again, we're gonna have the pool come up. You can share your answers with that. And if you have done that and if you have actually deployed an app, I would love to see that I would love to see that particular URL in the chat. So do share your project that you have actually white coded with the rest of the folks in here. And I see a lot of people have tried wipe coding. Some of them have, like, not done that, and I I see there's one word that says maybe, which is interesting. I see that someone is mentioning that 95% of their lines of code are all wiped. That's an interesting. Alright. Awesome. Awesome. I see a lot of people are sharing their projects. I love this. Keep this coming on over here. Now for you all, what I have done is I have prepared the slides. I have made the slides available so that you can follow it along with me. So if you scan this QR code or visit to that particular link, it should open the slides for you. Now that is also a fun little element I have tried to add in here is if you see over here, there are a few emojis. So if you have the slides open, you are on the slide the same slide as me. You can interact with the slides live. So if you click on that emoji, it will pop up. I see a heart going up. So it is gonna pop up in the slides. So this way, you can give me feedback about, you know, how you feel is the session is going, and I am gonna try to, you know, adapt to this particular feedback. Awesome. So let's move on. I am gonna do a quick introduction about myself. My name is Hershel. That's a book that I recently wrote with my coauthor, Tanay. I as I mentioned, I love to build things. So this is what this was kind of my way of sharing what I learned with running Raspberry LLMs on a Raspberry Pi. Now I work as a senior developer educator at Cloudflare, which means I get the opportunity to try out different things that the amazing engineers at Cloudflare build, learn how to use them, and then teach the wider community just like I am doing right now. And, again, I love to build things. These are a few apps that I have built. These are in productions. There are two oh, I would say one other app that I am gonna showcase at the end of the presentation, which uses the Sandbox SDK to give you more inspiration on what more you can do with the Sandbox SDK. So let's move towards the goal of today's today's session. I want you all to understand what is an isolated environment and how you can spin up an isolated environment with the Sandbox SDK. We're gonna then look into how you can generate code in this isolated environment using AI and execute it securely. We're gonna look into how you can manage secrets and dependencies. We're gonna talk about how you can implement production ready patterns. And, hopefully, by the end of this session, you will have more confidence of building real applications. Now a lot of this is like a stretch goal over here. My main motivation over here is the last thing you should be feeling confident after the session of going out and building an application using the Sandbox SDK. Alright. I'm gonna take a small pause over here, see what folks have in the chat before we move on to the next part. Okay. I see people are sharing their projects, which is interesting. Is the slide app built with Cloudflare dev platform? Kind of it is. I have a video on how I build the slides. You can always check it out on my YouTube channel. It uses the developer platform, obviously. Okay. Moving forward. Now we talked about AI coding very early on, but we kind of need to understand why it is promising and why people are adopting this day by day. It obviously loves us to build applications faster. Now the theme that you see for this slide is not hardcoded by me. I asked Claude to, hey. Go through the workers or cloudfed. Their website because I love the design for their website. So I asked Claw to go to their website, understand the code, understand the design of it, and then help me implement or that they implement it by itself on my slides. So, again, the slides are designed by AI. It's not designed by me. The content is mine, not not AI, obviously. Now because AI helps you write code faster, it helps you save things even faster. Now instead of, you know, releasing a feature in weeks, you can release features now in days because you are not wasting a lot of time writing the code by yourself. You just prompt the AI. You give AI more and more context about what is needed. And everyone can build. This is something I really love, that everyone over here now can build. They don't need to be now a developer like you or me Even if they know how to talk to an LLM, which I assume a lot of us do, they can build their applications. And someone already mentioned Lovable, which is a fantastic application to build your own websites with this prompt. But then the question arises, is it really safe? If you just like the AI build applications, is it really safe? I don't know how many of you follow the AI bubble very closely, but I do, and I have seen a lot of issues come popping up when people just white code their applications blindly. And this is not just white coding an application, but it is also writing or letting AI run the code on your bare metal servers. So if you think about it, you are, like, it's pretty chill when, you know, you white code an application. It's doing great. You get millions of users. You get, I don't know, 100 k a month you're making with that, which is fantastic. But behind the scenes, there is, like, this little cute cat which does not really know how to make your application secure. Right? So your security for your application is this little cat, which is scary. And there have been cases where there have been a lot of issues with white coded applications. So let's talk about these securities security issues and try to understand it. One of the major security issues that I have seen is data breach. What does this really mean? Well, if you let the AI coding tools access to your database or your production database, it is it can do things that you don't want. It can either delete all the records, it can modify the records, or it can even send out the records if someone does prompt injection. And then there is access to secrets. If you are saving your secrets anywhere where the AI can access this, it can access the secrets to make or use those secrets to make API calls. At the end of the day, you might get a hefty bill for that API. And that is a really, really big issue with file system access. I recently saw this. There is an amazing tool out there which lets you control or which lets you use AI tools from your phone or from anywhere. The only problem is it runs locally on your machine, which means that it has access to file system. And I saw a tweet where someone was trying to use it to manage that files, and the AI actually deleted the files which it should not have. So that's the major reason why you should be very careful when you run AI models on bare metals. Now, again, when I say bare metals, I over here mean both the server and your local machine. Remote code execution is really interesting. I'm gonna tell you, like, a kind of a story over here. So a big com a huge company, I think it was Datadog, if I'm not wrong. They were researching into how they can use AI and all the security flaws with that. So they asked one of the AI models to, you know, use Python to build an application. The AI model did well. It created a Python application, and things were working really well. Right? The application was working fine. They got the input and the output they expected. However, when they try to look more into the security aspect of it, they realized that the users can input a code by themselves, and that code gets executed on the server. Right? Now this is scary. Imagine if a user tries to execute malicious code on your servers. They can have access to any kind of data or they can hamper your application as well. So these are some of the security issues with running AI on bare metals. And there is a solution. The solution is to isolate it in something called a sandbox. So let's talk about what is the Cloudflare sandbox SDK. Well, the sandbox SDK gives you a secure environment where you can run the code, which means that each AI model can have its or not a model, but your each chat interface can have its own separate instance where the code that the AI writes gets executed in that particular isolated environment. It is built on top of CloudFare containers. If you didn't know this, yes, Cloudflare has containers now. You can run your Docker images on containers on the edge. So it's built on top of Cloudflare containers, and this Endpoint SDK provides a very simple TypeScript API. I think that this was one of the main reasons why I fell in love with the Sandbox SDK. We'll we'll do a hands on where you'll get a get a better understanding of why I love it and how simple it is to, you know, use the Sandbox SDK. And as I mentioned, it runs at the edge with workers. Now what does this really mean? Well, let's just say someone over here is trying to use your application, which is using the Sandbox SDK. Now they are you are based in India, and your user is trying to access it from Germany. The best thing is because it runs at the edge, whenever they make a request, the data gets shoved from a data center in Germany. And, also, the sandbox instance that gets created is gonna happen in a data center in Germany or around Germany. So the request is not traveling all the way to India, saving that network latency. And as I mentioned, it is it gives you an isolated environment, hence, it is perfect for running AI generated code. So to take a look at the architecture, this is what the architecture looks like. You make a call through the worker where your application logic lives. The Sandbox SDK gives you an extraction on top of durable objects and the container. I am not gonna dive a lot into durable objects. The good thing is sandbox SDK kind of abstracts away a lot of the durable object concepts. You don't really need to worry about it. But if you are interested in learning more about durable objects, I would encourage you to go to a documentation to get a better idea of it. And if you want us to do, like, a similar session on durable objects, do let us know in the chat so that the folks in the chat at the backstage can take a note of it, and we can arrange something as well. Alright. So you have the you have the table object for persisting state, and then you have a container, which is basically an isolated VM with a full Linux environment. Now we're gonna move on to the hands on part. This was, like, the kind of the theoretical part of it to give you an idea of, you know, how or what is Sandbox SDK, why you need to think about isolation, and stuff like that. We're gonna move into the hands on part, and there is obviously a prerequisite. You all must have got this in the registration email, is that you need to have Docker. You need to have Node. Js installed. You need to have a Cloudflare account. Now the free tier is gonna work fine for, like, the local development, but I would encourage you to, you know, have a paid tier if you want to deploy that application. And, hey, the paid tier is just $5 a month, which is, like, a good steal. So if you wanna deploy an application on the edge of which uses containers, I would encourage you to have the paid tier. You obviously need a code editor. I am gonna use Versus Code. You're free to use whatever code editor you want to use, and, obviously, we're gonna use a terminal over here. Alright. So the first thing we're do is we are going to set up our project repository and look into the basic code that is in the template and try to understand it. So what I want you all to do is I already shared the link of the QR code, but if folks need it again, let me know. I'm gonna share the get back to the slide where you can get the get the QR code for the slides so that you have the slides open and then follow along the instructions on the slides. And if not, if you if that is something, you know, that you feel is gonna be a bit tedious, I also have a GitHub repository. So we have a workshop GitHub repo. So if you go on the docs tab, that is that link for workshop GitHub repo. So if you click on that, it is gonna take you let me open it over here. It is gonna take you to this particular repository. So you can go to this repository. This repository, I have tried to add everything that is on the slides. I just wanna take you step by step on what we want to do. So the main branch is kind of exercise one, then we have other branches as well, which is exercise two and three. Now based on how we are doing with time, we are gonna move forward to the next exercises as well. Alright. So I'm gonna walk you all through the steps of how you can go ahead and create your own sandboxes. But what I am also gonna do is I'm gonna give you all a bit of time to, you know, run those code by yourself. Right? So, again, let me know in the chat if you need more time. I am continuously monitoring the chat as well so that we all are on the same page. Alright. So the first step to create a sandbox is to use the Cloudflare CLI command, which is n p m create Cloudflare. Over here, I am running the I am calling my project my sandbox. You can call it whatever you want. And then I am passing a template flag where I am asking it to use the example template that we have. So that's the command that we're gonna execute first to create a simple sandbox over here. Like, I see someone is asking where they can find the repo. So in your cold cast interface, there is, a chat tab, and then there is a docs tab. So if you click on the docs tab, that is where you can find the repo, and there it's called workshop GitHub repo. So that's where you can find the repo with all the instructions for the session. Alright. Once you run this, I just wanna ask you two particular questions. One is if you want to initialize initialize this as a git repository. I would say go ahead and do that so that you can commit your changes after each exercise. If And you want to refer back, you can refer back to those commits. And then there is if you want to deploy it to Cloudflare. For now, you can skip that step and say no. But if you want, you can go ahead and deploy as well. For now, I would leave the deployment. I would not deploy it because, again, we haven't added any kind of authentication in here. So if you deploy it right now and people have access to that URL, it might be that, you know, they can access it. They can access your application, and there might be new and new instances created for you. Alright. I already have the application up ready on my machine. So this is my application. And in here, you see that are a bunch of files and folders. We're gonna take a look step by step. So the first file we want to look is the index dot t s file, and this is where you are going to write your application logic. So I'm not gonna go to the code right over here. I'm gonna come to that later. But as you can see, this kind of contains the application logic for our sandbox. The next is the Dockerfile. This is where we define what Docker image we want to use. Again, the sandbox SDK comes with its own binary that you can use. But if you want, you can also use your own custom Docker image. And by default, it is exporting port eight zero eight zero. Now this is as it is required for local development, but there's another reason why we need it for now. And I'm gonna come to this probably in exercise three. Then there is the usual package dot JSON file, and we have a Wrangler dot JSONC file. Now this Wrangler config file is very particular to Cloudflare. Right? We have this is where you basically define what kind of other services you want to use with your application. Right? So because this is Sandbox SDK, as I mentioned, it it encapsulates both the containers and durable objects. So we already have the configurations or in the Cloudflare terms, bindings for that particular for the containers and durable objects. So in containers, we specify the type of container we want. We give it a class name. By default, it is sandbox. We are giving it we're specifying where the image lives. We're also specifying the instance type. As you can see, when I hover over, there are multiple instance type that you can use. Again, the perp for the purpose of this particular session, I'm gonna stick with the light. But based on your usage, you can, you know, select different instance type, and you can also specify how many maximum instances you want your application to run. The next is the configuration for durable objects. Again, specifying the name of or the class thing for the durable object and then the migration for that. Now, again, I'm not gonna jump into durable objects right now. This is already provisioned for you out of the box, so you don't need to worry a lot about it. Okay. But what if you want to use another service from Cloudflare? Right? How do you do the bindings? This was already created for you. Let's just say you want to use Workers AI, which is, again, a service from Cloudflare which allows you to run AI inference on the edge, and we have OpenShift models hosted on our own GPUs across the globe that you can use for your application. So to do that, what you can do is you do AI, and then I can add the bindings for AI, and I'm gonna do a capital alright. Yeah. A capital AI. That's how I like to refer these bindings. You can use so these are kind of variables. You can use it however you want. I like to use it with a capital AI. You can call it whatever you want. Alright. Now because I have done this, I I'm gonna run the command npm run c f type gen. This is gonna basically create the types for us, and these types, you can view in the worker configuration file. So in the worker configuration file, as you can see, there is the sandbox the sandbox type and the AI type configured for us. Alright. So this was the overview of the project. What I am gonna do now is move back to my slides and continue from over here. So I am gonna assume you all have the template provision. You have created the project, and you have installed the dependencies. The next step I want you all to do is start the local server with npm run dev. Now before you do that, make sure you have Docker running on your local machine. But if you don't, it is gonna throw an error. So make sure you have Docker up and running. For me, I have Docker running in here. So I have Docker up and running. I am gonna go to my terminal, do npm run dev. This is gonna start my application. It's gonna take a couple of seconds over here. As you as you can see now, it says preparing the container image, and it's gonna build that image for us. And it's done. Right? This is pretty quick because I already have run this a couple of times. Just just trying to, you know, use the cache for this. Now that I have this up and running, I can execute the endpoints. So let's just go back over here. I am going to copy this command. I am going to open another terminal. Let me clear this. Execute this, and I get an output, which is fantastic. So till now, we created we bootstrapped a repo from the templates. We executed the executed the command to execute the run endpoint, and we got an output. So far, we have got something which is functional. Now let's go and try and understand the code. Alright. So this is the code snippet, same as you see in the index. So t s y, I'm gonna go line by line to help you understand how it works. So the first thing we do is we import the get sandbox method, and we're gonna use this method to either create a new instance of the sandbox or use the instance of the sandbox that is already there. Right? So that's the first thing we do. Next, again, the sandbox SDK encapsulates durable objects. And if you are using durable objects, you need to export the durable object class, and that's what we are doing over here. Again, this is already done for you. You don't need to do anything in here. Next, in the fetch handler, the first thing we are doing is we are intercepting the incoming URL. We're gonna use this to understand or figure out which path the user is making a request. Once we have that URL, we are also creating the sandbox instance using the get sandbox method. Now over here, we are passing the env dot sandbox durable object and then also the ID of this particular sandbox. Now this over here should be unique for each user. For the purpose of this demo, we're just gonna use a single ID, which means that it is not gonna create the sandbox every time for this particular ID because it has already created the instance for this ID. It is going to use that instance again and again for the ID. Alright. Moving forward, we check the route if it is slash run. What we want to do is we want to execute some code, so we use the execute command in here, pass on what we want to execute, and then we send out the response to our end user. So that was the first part that we did. We ran the or we executed the slash run endpoint, and we got some output. Now let's try to execute the slash file endpoint. So I'm gonna copy this, come back over here, paste this, and we see some output in here as well. It says hello sandbox, and it gives me a bunch of more information. Now the expected response and the response I got is a bit different because in my example, I have modified the code a bit, and I I'm gonna show you that as well. Alright. So now let's try and understood understand what happens at the file route. So the Sandbox SDK gives you methods where you can write a file. So in here, you specify which is the what's the file name and where it is located. So I am adding it to hello dot t h c, and the next parameter is the content for this file. I am calling it hello sandbox. You can call it whatever you want, or you can basically have whatever content you want. And if you want to read the file, you can use the read file method over here, and you pass on the location of that particular file. And then we are simply sending out the response. Now, again, as I mentioned, in my code, I am doing one more extra method in here. So let me go to index. T s file, and I am using another method over here, which is called list files. Now this is gonna list the files in a particular directory, and I am sending it as a response back to the user. Alright. So moving forward, let's look at the core APIs that we saw and some of which we're gonna look at later. So if you want to execute any kind of commands, you can use the execute method. But if you want to run a long running process, think of this like a web server for your Node application or maybe a Python application. You would use the start process method over here. Now to interact with the files, we have three methods, write file, write files, read files, and then list files. And if you want to run the code, we have a run code method over here, which helps you execute either Python or JavaScript code as well. There is a difference between the execute command and the run code command. With the execute command, you cannot directly just, you know, write a JavaScript on and Python code and assume that it is gonna execute. It's that's not how it works. Think of the execute command as, like, your as a way to run the command that you can run on your shell or your terminal. Right? Whereas the run code command allows you to run the code for a particular language, for example, Python or JavaScript. Now in the next part, we are gonna look into how you can use the run code method, and we're also gonna look into how you can build an AI code executor. But before I do that, I'm gonna take again a quick pause to make sure that everyone over here is with me. Everyone is doing fine. They got they had a better understanding of the core APIs, the core for the minimal project, and that we can move forward to the next step. I saw a thumbs up going on. That's pretty fantastic. I see my wonderful colleague, Naresh, is also helping me over here with answering the questions. FYI, if you have any specific questions for Sandbox SDK, Narej is one of the engineers who is building it. So if you have any questions, this is the right time to ask them and kind of bug Naresh for the answers. I'm sorry, Naresh. Alright. Okay. Because I am not seeing any messages where, hey. Stop. I did not understand this. I'm gonna move forward with the next part of this session, which is gonna be exercise 2.1, use the code API. Now what I want you all to do is try to, you know, go to the docs, check how the run code API works, and try to add it to your example or to your application. Right? Again, this is all available in the GitHub repo as well. So what we did till now was the main branch was, like, the exercise one. If you go to exercise two, you will find the code or the complete solution for that as well. Yep. I I see that someone is asking for more time for for more time for doing the exercise. Don't worry. I'm gonna take a pause over here. I'm gonna give you all, like, five to seven minutes to do this. And I'm just gonna keep blabbering and give you, like, more tips, more ideas on how you can, you know, use the Sandbox SDK, how you can build with the Cloudify developer platform. Now earlier on, I asked how many of you use AI coding tools, and almost everyone mentioned that they do use that, which is fantastic. And you can actually use this You can actually use a couple of things that we provide to make it easy for you to use to use the to use your AI tools to build applications on us. So if you go to developers.cloudflare.com, there is a AI tools tab. If you click on that, you can see that we provide you with LLM dot TXT files. So you can basically just, you know, go to any page. For example, this particular page, you can copy the page link, view it as a markdown, or open it with, you know, whatever AI tool you're using, and you can pass the whole markdown to your AI application. That's one way to do it. The other way to do it is use MCP. I use this a lot. We have an MCP for our docs. So it's like docs.mcp.cloud5.com/mcp. You can configure this in your AI coding agents so that it always uses the correct information from us or from the docs and make sure that, you know, it is not hallucinating and giving you incorrect output. We also have skills, agent skills over here. It's not in the docs, but if you search on Google about Cloudflare agent skills, you should be able to you should be able to find them as well. So if you are someone who is using skills, that is another way of using that in your AI coding tools. Alright. Okay. I I see a message in the chat. Yes. The skills are still not in the docs, but it is we have the skills out there. I think the PR is also for the docs. It's gonna be on the docs in this particular page itself, but you can go ahead and find the skills. There is a lot of interesting work going on on the skills side, but I'm not gonna talk a lot because I don't wanna give spoilers over here. And, hey, this is not a talk about skills. We're gonna learn over here called the Sandbox SDK. Alright. I am curious to know how many of you have or were able to try out the run code command and get something out there. Okay. Okay. Okay. I see I have a feeling that folks are still doing it. So you know what? I'm gonna try to do that with you all now over here. I am gonna obviously cheat a bit because I don't wanna, like, just go ahead and write the code by itself all the time. So what I am gonna do is I'm gonna continue building on this. So I am gonna have a new path. I'm gonna call it code over here. And then what we want to do is we want to create a code code context over here. So I'm gonna do const c t x, which is gonna be my context, and this is gonna be sandbox again dot create code context. In here, I am going to specify which language I want it to execute. So I'm gonna do language and then specify JavaScript. And you can see there is Python as well. But if you want to do Python, you will have to update your Docker image over here. So over here, it is using the simple sandbox SDK docker image, which is fine, but it does not have Python installed. So you would have to use the Python version of this, and you and then you can execute the Python code. But for simplicity, I'm just gonna go with JavaScript for now. And then I am gonna do const result. This is gonna be equal to await sandbox dot. Now, again, the matter is called run code. And then what code do you want to run? Right? I am gonna try I'm just gonna keep it simple for now. I'm just gonna do console dot log and say hello from run code, and then pass on the CTX over here. So the context is gonna be this that we already have defined. Right. Once you have that, we can put in the response with response dot JSON, and I'm gonna do code, which is gonna be result dot code. So this is basically the code that we have passed or that we want to run. The next thing I wanna do is if there is an error, I want to return that. So I'm gonna do result dot error. And lastly is gonna be a simple result. I'm just gonna do result is equal to result dot logs dot h t t out. I'm gonna save this, and, hopefully, I know how to type things and I have typed things correctly. I can execute the code endpoint now. If I do slash code, we get the output hello from WinCode, which is fantastic. Right? So we wrote the code in JavaScript, and our sandbox executed that JavaScript written code for us. Right? Now I I'm gonna ask for you all for the feedback. How are you all doing with this? Do you want me to move forward, or do you need more time to use the run code method? Now over here, I know this particular session now is called, like, you know, using AI to write code, but we are writing the code ourselves. Don't worry. In the next exercise, we are gonna use AI to write the code. For us, what I want to do is, like, give you a bit of idea of how this is gonna work. So think of it like a building block. You are slowly trying to learn and understand different building blocks of this Inbox SDK so that you can then build a full fledged application. Alright, folks. Let me know in the chat if we are all up to speed and if we want to move on to the next steps. In the meantime, I am gonna check the questions. I see Nuresh has answered all the questions. Thank you again, Nuresh, for this. I don't think I have any questions to answer right now. I I see someone mentioned is streaming possible, and that is answered that it is. You can also so I did not cover it, but if I just do, like, just a sandbox dot just gonna do this. So you see there is, like, an execute stream, which is allow which is gonna allow you to stream the output, then there is run code stream as well. So there are so you can basically do streaming with this over here. Alright. I am gonna make a terrible assumption that you all have understood one code and were able to implement it in some shape or form. And I'm gonna move on to, again, like, just quickly go through the code. So what we saw over here is we create a code context. We pass on the language that we want to execute. We then pass on the actual code for that particular language that we want to execute in the run code method, and then we send send out the or send out the response back to the user. Now this is gonna be a bit more exciting, and I'm gonna give you all a bit more time over here. Is now we understood how you can run use the run code method to run the code and do something with it. We're gonna look into how you can use AI to actually generate the code for you. Now before we do that, let's take a step back and think about how we want our application to perform. Right? We don't want the code to be very static. We want the user to provide us an input. So the first step will be to take the user input into our application. Once we have that user input, the next step is to pass it on to an AI model. We pass it on to the AI model. The AI model generates the code for us. It gives us the code. Next step then becomes writing that code in the sandbox. And once that code is in our sandbox, we can execute that particular code. Right? So I'm gonna cover, I think, like, step one and step two. I would leave step three and four on you all to try out. So for step one, what you want to do is you want to have a new endpoint. You can continue with the code endpoint. But now because you want to take the user input, make sure you also check if it's, like, a post method. Right? So you make sure that you check it if it's a post method, and then you take in the user input. I am simply calling it as input. You can call it whatever you want. So take the user input from the request coming in. Once you have that, I want you all to pass on this to an LLM model. Now over here, I am using Workers AI. And earlier, we saw how you can add AI bindings for Workers AI for that. Right? So now because I already have the AI bindings, what I will do is I will basically use the run command on that particular binding, pass on the model name. Now I am using the GPT OSS model. You can use again whatever model you want from WorkersAI. Let me quickly show that to you. WorkersAI model. Now as I mentioned, there are tens and tens of models available that you can use with Workers AI. You can either also go with one twenty b as well. But, again, for purpose of this demo, I am gonna go with 20 b. There are all sorts of models. There is, like, a text to image model as well. There's an image to text model. There is speech recognition. There is also how do you call it? Voice generation or voice activity detection model. There are all sorts of models that you can use. For the purpose of this particular session, we need to use something that generates text. So you can filter this out with text generation model and check out what all models are available. Now, again, I am showing you how you can do it with Workers AI. You don't have to use Workers AI. You can use whatever AI model or AI inference engine you want to use. You can directly integrate this with the Cloud Code SDK or the Cloud SDK. You can also use OpenCode if you want. We already have examples of all those AI coding agents. So, again, make sure you check out the examples repo where you can find, you know, the implementation of all this. You can also use AI gateway. That is something that I have played around with as well. So that is another in that is another way of how you can interact with LLM models for your application. Okay. So what I am going to do is because I showed you both step one, which is taking the user input, and step two, which is generating or passing that user input to an LLM to generate the code. I'm gonna implement this myself and give you all time to move on to step three, which is gonna be basically writing this code or how do you call it? Yeah. Writing this code in the sandbox and then executing it in step four. Right? So let me go on to my code editor, and I am gonna create a new route. URL dot path name. This is gonna be slash vive. And as I mentioned, I am gonna check for the method. So request dot method should be post. Alright. Now the first thing we want to do is take the input. So I'm gonna say const input equals await request dot JSON. I'm gonna not worry a lot about TypeScript over here. Alright. So we have that. The next step is to pass it on to an LLM. Now, again, I already have this binding configured, so I can do const result or let me call it generated code equals await env dot a I dot run, and then I can do here GPT 20 b. And it takes an instructions. I'm gonna pass it pass it an instruction. Now I'm gonna give it, like, a very simple, I would say, system prompt or an instruction, and I'm gonna show that, yeah, over here. So what I'm saying is, like, hey. You are a JavaScript developer. Based on the user input, you generate secure and correct JavaScript code. You are responsible to only generate the code. Do not respond with anything else. Now this is not the best system prompt over here, but it gets the job done. So we I am gonna use this. Again, for production, make sure you have a better, more accurate system prompt. And the next thing is I I'm gonna pass the input over here. So this is gonna generate some content. Let us, for now, just return that. Return response dot JSON, and I'm gonna just simply do send it that code. Alright. What I am gonna do is I'm gonna open the Postman extension, and I have localhost eight seven eight seven. This is where my application is running. I'm gonna go to slash wipe endpoint. And in here, I am gonna have the input, and I'm gonna share write code to add to numbers. Right. I'm gonna close the terminal so that we have more window over here. And we got some response. So this is the LLM trying to do its reasoning. As you can see, this is the reasoning text, and then we have the actual output from the LLM over here. I'll try to zoom in a bit more so that you all can see it more clearly. So we have the code coming in from the LLM over here, which, in my opinion, is fantastic. Now I don't have to actually write the code by myself. Right? But as you can see, there is a lot of other stuff that the other one gives or the response gives us over here. There is, like, this ID. There is, like, you know, the reasoning text, then there is all the other information, which I don't really need. What I need is just this particular line of code. Now what I have done for you all is created a simple function that you can simply copy and paste, which is the extract code from response. So I'm gonna copy this function and paste it on in here, and we're use this function to just extract the code from LLLM. So if I do extract code from response, and I now pass it the generate generated code, This should give me an error, which is fine. Again, I'm not gonna focus on TypeScript over here. The code is the code already has a TypeScript. Okay. I'm just gonna say code. Alright. The server reloaded. I'm gonna come back over here. They send the same request, and we should now hopefully just see the code. Just gonna go back over here, and now we see that it is just giving us the code. So this is fantastic. We took the user input. We passed that user input to an AI model as the AI model generate a code for us, and now we have that code. Right? Now this is what I talked earlier in step one and two. Now there is what I want you all to do is try to write this code to the sandbox so that you can execute this code. Now there are two ways kind of two ways how you can do it. I have both the ways again in the example repo where you can see that. Also, I am trying to, you know, be aware of the time and be mindful of that. So I'm gonna have to kind of pick up a bit of pace after this. So I'm gonna give you all, like, three more minutes to try to, you know, run this, execute the AI generated code. And if you don't worry if you are not able to. All the code is over here, and I'm gonna just gonna explain you how it works. Alright. Two more minutes. I am gonna see if there is any things in the chat. I see Nareh's posted also the call command. Thank you for that. You can you can also use the call command. That's I should have added it. My bad. But, yeah, you can do it with call command, or you can use tools like Postman, Insomnia. There are a bunch of tools out there. Okay. What I am gonna do is wait for one more minute because I wanna make sure you all are able to follow with me and you are able to, you know, write the code for itself. One thing I would want to mention is if you have multiple CloudFare accounts, it might ask you when you use Workers AI to configure or select which CloudFare account you want to execute this against. Now don't worry. It's not gonna charge you. There is, like, a generous fee tier even for this AI models. So for this workshop, we are gonna be in that in that particular limitation. So you don't have to, you know, worry about, like, getting a bill from us after this. So that's one thing that you should definitely note. So if you get that pop up in your c in your terminal, make sure you select an account that you want to use for this. And alright. So I'm gonna move on to the next part of this particular exercise itself, which is I need to refresh this. Okay. So how do you write the code to the sandbox? Now as you all learned in exercise one that you can use the write file command. So what here I have done is just once I get the extracted code, I use the write file command to write this code to an index dot j s file. And once we have that file, we can use the execute command to execute that particular file. Now, again, because this is Node, I am gonna do Node and then specify the path for that file, and this is gonna execute the code for us. Right? So as I mentioned, there are two different ways. This is the first way of doing it. The other way of doing it is using the run code method. Again, if you go on to the repo, go on to exercise two, and that shows an index dot t s. I have specified how you can use the run code method. Right? So, again, we create a context the code context for this. And now instead of writing the code that we extracted from the LLM response, we pass it on to the run code method, and this is gonna execute the code for us. And then we simply return this back to our end user. So why I am showing you the write file method and the run code method? Now you might be building an application where you want to run a code just once, and you don't really need to write it to a file, which is fine, and that's where you should be using the run code method. But if you are building, like, a white coding platform where you want to build an application where AI generates the code and you actually have to write that code in a file. Think of this as a React application, for example. Right? You have an application in React where you have different files which serve different purposes. You write components in a particular file. You write the pages whole logic in a particular file, and that's where you want to actually use this particular format where you write the file. And once the files are written, you start some kind of a process. Alright. So I am gonna move on to the next part of it, which is trying to understand the security best practices for it. Because we now have, I believe, a better understanding of how to use the Sandbox SDK. Right? But we also need to talk about how to make sure that we are following the best security practices when we build an application with this. So the first thing we obviously want to do is validate the input that the user sends. This is really important if you are trying to directly execute the code that the user has that you're taking in from the user. So make sure you do input validation for any messages that are coming in from the user. This is a very simple example. Based on the application, you can have more complex logic for input validation. The other important thing that I mentioned very early on is having a separate sandbox instance for each user. For our demo, we are just creating a sandbox with an ID, my sandbox. But if you are actually using this in production, you want each user to have its own separate sandbox instance. And the way to do this is to create a a unique sandbox instance by passing their user ID. And the reason this isolation is really important is because once you isolate this per user or per chat or however you want to do it, you make sure that that the file system in one instance is not readable or else or or no other sandbox can actually talk to each other and, like, access their file system. There's also process isolation, which means that if one sandbox instance has a web server running, data sandbox instance is not gonna know about that. There is also network isolation. So if you want so the those two sandboxes cannot talk to each other, and each sandbox is gonna have its own resources, which makes sure that, you know, you are your sandboxes are instantiated correctly. So we saw the input validation. We saw why you should have isolation. The next thing we should be talking about is how do you manage environment variables. Now in the sandbox SDK, there are three different ways to do it. The first is to set it at the global sandbox level. Now, again, this is gonna be isolated per user, so it's not gonna be, like, two sandboxes can can access the environment variable of each other. It's now it's not gonna work. And the way to configure this is use the set ENV wise method where you can basically specify the the environment variable on how you want to do it. And there is the per command level with the execution, and then there is the production sorry. And then there is the session level create session as well. Now if you think about session, the sessions are mainly think about the sandbox as a computer and the session as different terminal tabs. So each session can have its own kind of think of it, like, as each terminal. And because it's a terminal, it is kind of, you know, able to use only the things that you configure in that particular session. And never hard code this never hard code your secrets in the files. Now Mahesh has created a fantastic example on how Vue can configure secrets. TLDR, it is proxying it through the worker. But if you wanna look at the code, again, the link is in the slides. Alright. Now we are running out of time. So what I am gonna do is that I'm gonna walk through the way of actually building an application where you can also preview an app. So if you think about how we do development currently is we clone the repository, we install the dependencies, we run AI agent inside the sandbox. Think of it like running the AI agent on a terminal to write the code. Once that is done, we start the development server, and we then proxy the URL or we view the URL on the browser. Right? So how does this replicate in the sandbox environment? Well, we first clone the repository using the git checkout command. In the git checkout command, you can specify what you want to call the directory. You can also specify the particular branch in here. One thing I have learned while using this a lot is you make sure that, you know, you don't reclone the repo. So you always have a check-in there because on each request, what I used to do is just clone the repo, and it would give me an error. So make sure you have a check-in there for this. Alternatively, if, you know, you have the application which is always gonna clone the repository, you can just add it to your Docker Docker file where it's gonna clone this in the Docker image itself. You don't do this again. Alright. The next thing is previewing an app is installing the dependencies. So once we have the once we have cloned it, we use the npm I command, which is npm install, to clone the depend to clone the dependence oh, sorry. To install the dependencies, we pass it on to we pass it on the directory where we want to actually execute this command. And then the dependencies are cloned. The last step is to start the development server. Now for this, what we are gonna use is we are gonna use the start process method over here because this is gonna be a long running server. So we're gonna do n p m run dev. Again, specify the directory, and this is gonna start our server. Now if you are building apps, you might want to have logs as well. So you can stream the logs using the stream process logs method over here, where I am passing the server ID, which I got when I run the start process command. And then I am using the wait until command to simply log this to my console. And this is gonna, you know, just continuously stream the output, but also make sure that the code executes on the next steps as well. Alright. The server is running, but we are not actually able to view our code because it is gonna be local host 8080 within the sandbox environment. So what we need to do is proxy the URL, and for that, we have to expose the port from the sandbox. So we use the export port command. Now my application is gonna run on port eight zero eight zero, and I need to pass on the host name, so I'm gonna do that. And remember, I mentioned that you need to expose the port in your Dockerfile, and that's where this comes from. Once you expose this port, you need to add two more lines of code in your worker so that you can proxy that request. And this is cons and this is as simple as this. So you have a proxy request. You proxy that to the sandbox, and it checks if it really needs to proxy the request. If it does, it is gonna proxy that request to your sandbox, which means that your user will be now able to go to this particular URL and view your application. But if it's not needed, your worker logic is gonna continue to run. So to quickly recap, we saw the core APIs. We looked into how you can use AI to generate the code. We also looked at how you can generate preview URL. Now, again, because of the interest of time, we could not do the x s three, but I would encourage you all to go through the GitHub repo and try to do it yourself. And if you have if you run into any kind of issues, feel free to hit me up. I would be, you know, happy to answer those questions. Now I'm gonna quickly go through the sandbox hygiene. Make sure you always clean up the resources. Use the destroy command over here. The reason being that you are not, you know, leaking any kind of resources. You are saving on cost, and you ensure that every time the user access that particular instance, it is on a clean state. So to give you an idea, this is what the life cycle of the sandbox looks like. You create or get a sandbox instance using the get sandbox command. You use that sandbox to run it, to execute commands, to manage files, whatever you want to do, and then you clean up using the destroy command. Going to the production checklist, make sure you follow it before you deploy it to to production. Validate user inputs. Always use a separate sandbox. Don't use a single sandbox for everything. Never hard code secrets, and implement proper authentication and rate limiting. Again, because of the interest of time, I did not cover all of this, but we already have example reports out there which you can use to get inspiration from. And to make sure you have better performance, make you all make sure you always clean up your sandbox with the destroy command. Use the keep alive method for long running processes, and you monitor the resources of usage. And for reliability, handle your errors gracefully. Make sure you are giving proper output to the user. You have set time also for the command, and you log and audit the operations. Now this is the basic authentication pattern. If you the user is not authenticated, send them an o four a four zero one. But if it is, give them access to their sandbox. So what did we learn today? Well, we learned this SDK's fundamentals. We learned how to run secure code execution that is generated by AI. We looked into the security best practices. We talked about the production patterns. And, hopefully, you feel a bit more empowered to build a real world application that gen that uses AI. So for the next steps that you can do is make sure you go to exercise three, finish exercise three as well, and then try to deploy your application with the command n p x wrangler deploy. And as I mentioned, we have tons of resources for you. So there are different examples for your different use cases, so go ahead and check them out. Join the community. There is a lot of chatter happening on our Discord server. Go ahead. Join the Discord server. See what other people are building and how you can build with them. And the documentation is my favorite resource as well. The documentation over here, there are examples. One thing I did not add over here, but we also have a developer YouTube channel where you can learn how we are using it, and we take a step by step on how you can build stuff. So thank you so much for joining in today's session. I hope this was fruitful. If there are questions, do let me know. I would love to take them. We have I I know I went a bit over time. Apologies on my side. I'm what I'm happy to take some questions, and I also wanted to show you two little demos that I am building. One of them is running a Remotion in the sandbox SDK, which means that a user can basically type, like, generate a video animation for x y z, and then the AI generates it using Remotion in the sandbox environment. So that's one of the experiments one of the demos that I am doing. Still, like, a early POC that I have. But another one I am building as kind of a product is, like, Slides AI or AI Slides where you basically prompt AI to generate the slides for you, and it is gonna generate the slides for you. It is using slide dev under the hood to generate the slides, and it is gonna also give you a preview as you see over here. Alright. So these were two things that I am currently building with the Sandbox SDK. I would love to see what you build with the Sandbox SDK. And once again, we have a couple of minutes for question, so feel free to post your question in the chat. Thank you all for joining in. If there are no questions, I really appreciate you all joining in today and spending some time with me learning about CloudFresh Inbox SDK. And, again, if you have any questions, you can find me on the Internet. Just go to hushel.dev, and you can find me on LinkedIn, Twitter, Blue Sky, wherever you are. I I see a question around pricing. This is a really good question. I know there is somewhat going on around with the pricing. I am not the best person to talk about pricing because I don't work in sales or marketing. I am like a developer like you all. So I am not gonna I cannot comment on the pricing. I would say whatever you see on the docs are, like, the current price pricing. But if you have any feedback that you want us to pass on to the team, do let me know. I am gonna capture this as a feedback and pass it on to the team. Alright. I am not seeing any more questions popping in. I'm gonna give, like, one more minute over here. But, again, the slides, as you can see, scan the QR code or just go to the link. You have the slides. You have the GitHub wrapper with you. You have the docs with you, and you have me with you. So feel free to, again, you know, like, try it out yourself, try to build applications. And if you run into any kind of issues, come to our Discord server, ask questions. The community is super engaging. So if not me, someone from the community is gonna help you out, and you can always also directly reach out to me. Awesome. Thank you all once again for joining in, and I hope that this was really useful. And I hope to see you all soon in session in the near future. Thank you, and take care.