{"id":10372,"date":"2023-06-23T01:43:43","date_gmt":"2023-06-23T08:43:43","guid":{"rendered":"https:\/\/www.couchbase.com\/developers\/tutorials\/quickstart-netlify\/"},"modified":"2023-06-23T01:43:43","modified_gmt":"2023-06-23T08:43:43","slug":"quickstart-netlify","status":"publish","type":"tutorials","link":"https:\/\/www.couchbase.com\/es\/developers\/tutorials\/quickstart-netlify\/","title":{"rendered":"Quickstart using Couchbase Capella and Netlify Serverless Functions"},"content":{"rendered":"\n<h3 id=\"h-overview\" class=\"wp-block-heading\">Overview<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The movement of <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/www.netlify.com\/jamstack\/\">static websites with serverless functions<\/a> is gaining momentum due to their simplicity, performance, and painless deployments. With static websites, there is no need for a dedicated server to process dynamic content as all the pages are pre-built and served directly to the browser. This means that serverless functions can be used to handle dynamic content, such as authentication and business logic, without needing to care about infrastructure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Netlify is a platform that is at the forefront of this movement and has made it extremely easy and friendly for developers to deploy their applications. With features such as automatic continuous deployment, built-in CDN, and serverless functions, Netlify makes it easy for developers to build and deploy static websites quickly and efficiently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Couchbase Capella follows the popular movement towards <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/www.couchbase.com\/blog\/what-is-a-database-as-a-service-dbaas-why-is-it-important\/\">Database-as-a-Service<\/a> and works very well with this paradigm of moving to cloud services. Couchbase Capella is fully managed and removes the pain of hosting your own dedicated servers. Capella offers developers performance, security, and high availability across many regions and cloud providers, with all the benefits of a NoSQL database. This enables developers to focus on building applications, increasing productivity and reducing costs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This tutorial will show how to quickly create a web application that utilizes Netlify Functions to connect to Couchbase Capella. We will be writing a web application that displays and mutates sample airline data in a Couchbase Capella database.<\/p>\n\n\n\n<h3 id=\"h-why-use-netlify-with-couchbase-capella\" class=\"wp-block-heading\">Why use Netlify with Couchbase Capella<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Using Netlify with Couchbase Capella can offer powerful benefits, including:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\n<p>Easy Deployment: Netlify offers a simple and straightforward deployment process for web applications, allowing you to easily deploy your application and manage it from a single dashboard. You can also integrate with Couchbase Capella to deploy your application alongside the database service.<\/p>\n<\/li>\n\n\n<li>\n<p>Scalability: Couchbase Capella is designed for cloud-native applications, and it offers scalability and flexibility to grow with your application needs. Netlify also supports auto-scaling, allowing your application to handle high traffic without any downtime.<\/p>\n<\/li>\n\n\n<li>\n<p>Performance: Couchbase Capella offers low-latency and high-throughput data access, which can significantly improve the performance of your web application. Netlify also provides features such as CDN integration and asset optimization, which can further improve the speed and performance of your application.<\/p>\n<\/li>\n\n\n<li>\n<p>Security: Couchbase Capella offers robust security features, including data encryption, role-based access control, and compliance with various data protection regulations. Netlify also offers security features such as SSL encryption to ensure your application is secure.<\/p>\n<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Overall, using Netlify with Couchbase Capella can help teams and individual developers easily deploy and manage a high-performance, scalable, and secure web application of any size.<\/p>\n\n\n\n<h3 id=\"h-prerequisites\" class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.couchbase.com\/cloud\/get-started\/create-account.html\">Couchbase Capella<\/a> account (Get started for free with a trial database account)<\/li>\n\n\n<li>A <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/www.netlify.com\/\">Netlify<\/a> account (Get started for free by signing in with your GitHub account)<\/li>\n\n\n<li>A GitHub account<\/li>\n\n\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.netlify.com\/cli\/get-started\/\">Netlify CLI<\/a><\/li>\n\n\n<li>Node.js 16+ setup and work with the CLI of your choice.<\/li>\n\n\n<li>curl or your preferred API testing tool<\/li>\n\n<\/ul>\n\n\n\n<h3 id=\"h-sample-application\" class=\"wp-block-heading\">Sample application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The final sample application can be found at on <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/netlify-tutorial\">GitHub<\/a> which can be used for reference.<\/p>\n\n\n\n<h3 id=\"h-set-up-your-couchbase-capella-database\" class=\"wp-block-heading\">Set up your Couchbase Capella database<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this section, we will walk through how to set up Couchbase Capella with sample data called <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.couchbase.com\/ruby-sdk\/current\/ref\/travel-app-data-model.html\"><code>travel-sample<\/code><\/a>. If you do not have a Couchbase Capella account, follow the directions in this <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/www.couchbase.com\/blog\/get-started-couchbase-capella\/\">blog article<\/a>. Please ensure you can sign into your Couchbase Capella account before continuing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We need to set up a Couchbase Capella database with the sample data and configure the internet and user settings to allow Netlify Functions to access the database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/www.couchbase.com\/blog\/get-started-couchbase-capella\/\">Create your Couchbase Capella database<\/a> and set up the sample data.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>On the Couchbase Capella console, create a new database and navigate to it.<\/li>\n\n\n<li>Click Import Data under the Start tab<\/li>\n\n\n<li>Import the Travel Sample dataset<\/li>\n\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"assets\/import_sample_data.png\" alt=\"import sample data\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we need to set up internet settings to allow connections from all IP addresses. This is required for Netlify Functions to access the database for the purpose of this tutorial.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigate to the <code>Settings<\/code> tab on the database page.<\/li>\n\n\n<li>Navigate to <code>Networking &gt; Internet<\/code> on the sidebar<\/li>\n\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">NOTE: We will use the connection string displayed on this screen later in our code that will allow us to configure how to connect to Couchbase Capella via the Couchbase NodeJS SDK. Please copy this information and put it somewhere where you can access it later in the tutorial to save yourself from returning to this screen.<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Click <code>Add allowed IP<\/code><\/li>\n\n\n<li>Allowlist <code>0.0.0.0\/0<\/code> and save<\/li>\n\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"assets\/internet_settings.png\" alt=\"internet settings\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we will need to create a user account that can be used to access the database from our Serverless Function.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Confirm that you are on the <code>Settings<\/code> tab.<\/li>\n\n\n<li>Navigate to <code>Security &gt; Database Access<\/code> on the sidebar<\/li>\n\n\n<li>Enter a memorable name and password as we&#8217;ll need that later.<\/li>\n\n\n<li>Select <code>travel-sample<\/code> for Bucket<\/li>\n\n\n<li>Select <code>All Scopes<\/code> for Scopes<\/li>\n\n\n<li>Select <code>Read\/Write<\/code> for Access<\/li>\n\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"assets\/internet_settings.png\" alt=\"database access\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You should now have a Couchbase Capella database, the <code>travel-sample<\/code> data loaded, network access configured, and a user account to access the data setup.<\/p>\n\n\n\n<h3 id=\"h-building-the-web-application\" class=\"wp-block-heading\">Building the web application<\/h3>\n\n\n\n<h4 id=\"h-create-a-new-netlify-project\" class=\"wp-block-heading\">Create a new Netlify project<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new directory for your project and navigate into it. Set up a GitHub repository.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you haven&#8217;t already installed the Netlify CLI:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm i -g netlify-cli\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new Netlify project using the command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>netlify sites:create\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Select the default team or your organization&#8217;s team for Team<\/li>\n\n\n<li>Leave the site name blank or type any name for Site name<\/li>\n\n<\/ol>\n\n\n\n<h4 id=\"h-install-dependencies\" class=\"wp-block-heading\">Install dependencies<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll need to install the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/www.npmjs.com\/package\/couchbase\">Couchbase SDK<\/a> and <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/www.npmjs.com\/package\/dotenv\">dotenv<\/a> in order for our Netlify Functions to connect to our database.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install couchbase dotenv\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-create-a-netlify-toml-file\" class=\"wp-block-heading\">Create a netlify.toml file<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">For the Couchbase SDK to work with Netlify, we&#8217;ll need to add a custom build command. Create a file called <code>netlify.toml<\/code> in the root directory and add the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[build]\n  command = &quot;strip --strip-debug .\/node_modules\/couchbase\/build\/Release\/couchbase_impl.node&quot;\n  publish = &quot;.&quot;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For more information on file-based configuration, please check out the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.netlify.com\/configure-builds\/file-based-configuration\/\">Netlify Docs<\/a><\/p>\n\n\n\n<h4 id=\"h-create-a--env-file\" class=\"wp-block-heading\">Create a .env file<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Hard coding your username and password into files and then checking that code into a repository like GitHub is highly discouraged. For the sake of this tutorial, we will need to create an environment file (<code>.env<\/code>) that includes the information on how to connect to our Couchbase Capella account and data. Later we will exclude this from being checked in. In real-world situations, you should inject these values via a CI\/CD pipeline using Netlify and environment variables.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the root directory of your project, create a <code>.env<\/code> file with the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>COUCHBASE_USERNAME=&lt;DATABASE_ACCESS_USERNAME&gt;\nCOUCHBASE_PASSWORD=&lt;DATABASE_ACCESS_PASSWORD&gt;\nCOUCHBASE_ENDPOINT=&lt;DATABASE_ENDPOINT&gt;\nCOUCHBASE_BUCKET=travel-sample\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Fill in the details with the database access username and password you set up earlier. Set the connection string we took note of earlier as the endpoint. This is required for testing our code locally.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>NOTE:<\/strong> Optionally, you can set environment variables via the CLI.  The CLI can also set the environment varibles for the app when it&#8217;s deployed.  More information can be found in the <a href=\"https:\/\/cli.netlify.com\/commands\/env\/\">Netlify documentation<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<h4 id=\"h-create-a--gitignore-file\" class=\"wp-block-heading\">Create a .gitignore file<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In the root directory of your project, create a <code>.gitignore<\/code> file so we don&#8217;t push our environment variables and build files to the GitHub repository.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Local Netlify folder\n.netlify\n.env\n\nnode_modules\/\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-create-serverless-functions\" class=\"wp-block-heading\">Create serverless functions<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll need to create some serverless functions to allow your web application to interact with the Couchbase database. This should be easy, thanks to the powerful CLI tool that netlify provides. We&#8217;ll create two functions for this tutorial that use the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.couchbase.com\/ruby-sdk\/current\/ref\/travel-app-data-model.html#airline-documents\">Airline documents<\/a>. The <code>get_airlines<\/code> function will run a query to retrieve some airline data, while the <code>put_airline<\/code> function will modify some airline data in our database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First create the <code>get_airlines<\/code> function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>netlify functions:create get_airlines\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Select Serverless function<\/li>\n\n\n<li>Use the default path (hit enter)<\/li>\n\n\n<li>Select Javascript<\/li>\n\n\n<li>Select hello world example<\/li>\n\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This will create a new <code>get_airlines.js<\/code> file with a <code>handler()<\/code> function.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Repeat the process to create a <code>put_airline<\/code> function.<\/p>\n\n\n\n<h4 id=\"h-write-the-getairlines-js-handler\" class=\"wp-block-heading\">Write the get_airlines.js handler<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Navigate to the <code>get_airlines.js<\/code> file you&#8217;ve just created.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With just a few lines of code, we can write an API to use the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.couchbase.com\/nodejs-sdk\/4.1\/hello-world\/start-using-sdk.html#connection\">Couchbase NodeJS SDK<\/a> to connect to our newly created database and query data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At the top of the file we can use the Couchbase SDK and our environment variables to initialize the Couchbase client. Note that this should be placed outside the handler, otherwise we will be creating a new connection to the database with every request.<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>const couchbase = require(&#039;couchbase&#039;)\nrequire(&#039;dotenv&#039;).config()\n\nconst ENDPOINT = process.env.COUCHBASE_ENDPOINT\nconst USERNAME = process.env.COUCHBASE_USERNAME\nconst PASSWORD = process.env.COUCHBASE_PASSWORD\n\nconst couchbaseClientPromise = couchbase.connect(&#039;couchbases:\/\/&#039; + ENDPOINT, {\n  username: USERNAME,\n  password: PASSWORD,\n  timeouts: {\n    kvTimeout: 10000, \/\/ milliseconds\n  },\n})\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now we need to edit the handler to use the Couchbase client to query the database for airline data and return the results.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet will query the database and return 5 airline entries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>const cluster = await couchbaseClientPromise\nconst results = await cluster.query(\n  &#039;SELECT * from `travel-sample`.inventory.airline LIMIT 5&#039;\n)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After some more modifications to the handler function to accept only GET requests and handle the response, your final handler should something look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>const handler = async (event) =&gt; {\n  \/\/ only allow GET requests\n  if (event.httpMethod !== &#039;GET&#039;) {\n    return {\n      statusCode: 405,\n    }\n  }\n\n  try {\n    const cluster = await couchbaseClientPromise\n    const results = await cluster.query(\n      &#039;SELECT * from `travel-sample`.inventory.airline LIMIT 5&#039;\n    )\n\n    return {\n      statusCode: 200,\n      body: JSON.stringify(results),\n    }\n  } catch (error) {\n    return { statusCode: 500, body: error.toString() }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To test our handler, we can run the netlify development environment with this CLI command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>netlify dev\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"assets\/netlify_dev.png\" alt=\"netlify dev\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Your Netlify development server should be up and running.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">We will use the url shown in the terminal throughout this tutorial in order to test our code. The port shown here may be different than on your machine. We will be using <code>http:\/\/localhost:8888<\/code> for the purposes of this tutorial, but you will need to replace the url in the commands with the one shown in your terminal.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">The function we just wrote will be hosted at <code>http:\/\/localhost:8888\/.netlify\/functions\/get_airlines<\/code>. The port may be different on your machine. It should return some airline data. Try making a request (Replace the port with the port of your development server if needed):<\/p>\n\n\n\n<pre class=\"wp-block-code language-sh\"><code>curl &#039;http:\/\/localhost:8888\/.netlify\/functions\/get_airlines&#039;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">or for Powershell users:<\/p>\n\n\n\n<pre class=\"wp-block-code language-sh\"><code>Invoke-RestMethod -Uri &#039;http:\/\/localhost:8888\/.netlify\/functions\/get_airlines&#039; | ConvertTo-Json -Dept 10\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-write-the-putairline-js-handler\" class=\"wp-block-heading\">Write the put_airline.js handler<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We also need to create a <code>put_airline<\/code> function to make changes to our database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This function will create a simple API to change the country of the 40-Mile-Air airline.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Copy the following into your <code>put_airline.js<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>const couchbase = require(&#039;couchbase&#039;)\nrequire(&#039;dotenv&#039;).config()\n\nconst ENDPOINT = process.env.COUCHBASE_ENDPOINT\nconst USERNAME = process.env.COUCHBASE_USERNAME\nconst PASSWORD = process.env.COUCHBASE_PASSWORD\nconst BUCKET = process.env.COUCHBASE_BUCKET\n\nconst couchbaseClientPromise = couchbase.connect(&#039;couchbases:\/\/&#039; + ENDPOINT, {\n  username: USERNAME,\n  password: PASSWORD,\n  timeouts: {\n    kvTimeout: 10000, \/\/ milliseconds\n  },\n})\n\nconst handler = async (event) =&gt; {\n  \/\/ only allow PUT requests\n  if (event.httpMethod !== &#039;PUT&#039;) {\n    return {\n      statusCode: 405,\n    }\n  }\n\n  try {\n    const cluster = await couchbaseClientPromise\n    const bucket = cluster.bucket(BUCKET)\n    const scope = bucket.scope(&#039;inventory&#039;)\n    const collection = scope.collection(&#039;airline&#039;)\n    const modifiedAirline = {\n      callsign: &#039;MILE-AIR&#039;,\n      country: JSON.parse(event.body).country,\n      iata: &#039;Q5&#039;,\n      icao: &#039;MLA&#039;,\n      id: 10,\n      name: &#039;40-Mile-Air&#039;,\n      type: &#039;airline&#039;,\n    }\n\n    const idToUpsert = modifiedAirline.type + &#039;_&#039; + modifiedAirline.id\n\n    const result = await collection.upsert(idToUpsert, modifiedAirline)\n\n    return {\n      statusCode: 200,\n      body: JSON.stringify(result),\n    }\n  } catch (error) {\n    return { statusCode: 500, body: error.toString() }\n  }\n}\n\nmodule.exports = { handler }\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can test our newly created function now. Make sure you&#8217;re running the <code>netlify dev<\/code> server. Your new function should be hosted at <code>http:\/\/localhost:8888\/.netlify\/functions\/put_airline<\/code>. The port may be different on your machine, so check the terminal for your url. Try making a PUT request to the API locally to change the country of the 40-Mile-Air airline to &#8220;United Kingdom&#8221; (Replace the port with the port of your development server if needed).<\/p>\n\n\n\n<pre class=\"wp-block-code language-sh\"><code>curl -H &quot;Content-type: application\/json&quot; -d &#039;{&quot;country&quot;:&quot;United Kingdom&quot;}&#039; &#039;http:\/\/localhost:8888\/.netlify\/functions\/put_airline&#039;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">or for Powershell users:<\/p>\n\n\n\n<pre class=\"wp-block-code language-sh\"><code>$headers=@{}\n$headers.Add(&quot;Content-type&quot;, &quot;application\/json&quot;)\n$body = &#039;{&quot;country&quot;:&quot;United Kingdom&quot;}&#039;\nInvoke-RestMethod -Uri &#039;http:\/\/localhost:8888\/.netlify\/functions\/put_airline&#039; -Method PUT -Headers $headers -ContentType &#039;application\/json&#039; -Body $body\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The new data should be reflected in the Couchbase Capella console or you can check the <code>get_airlines<\/code> API as well to see that the data has changed.<\/p>\n\n\n\n<pre class=\"wp-block-code language-sh\"><code>curl &#039;http:\/\/localhost:8888\/.netlify\/functions\/get_airlines&#039;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">or for Powershell users:<\/p>\n\n\n\n<pre class=\"wp-block-code language-sh\"><code>Invoke-RestMethod -Uri &#039;http:\/\/localhost:8888\/.netlify\/functions\/get_airlines&#039; | ConvertTo-Json -Dept 10\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With this, both our Netlify Functions are done.<\/p>\n\n\n\n<h4 id=\"h-write-the-frontend-for-our-web-application\" class=\"wp-block-heading\">Write the front-end for our web application<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Now that the serverless functions are done, we can write some HTML and Javascript to consume the APIs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The important snippets in this section to note are:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>await fetch(&quot;\/.netlify\/functions\/put_airline&quot;, {\n  method: &quot;PUT&quot;,\n  headers: {\n    &quot;Content-Type&quot;: &quot;application\/json&quot;,\n  },\n  body: JSON.stringify({ country: country }),\n});\n...\nlet results = await fetch(&quot;\/.netlify\/functions\/get_airlines&quot;);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can make requests to the serverless functions we created with just these few lines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create an <code>index.html<\/code> file in the root directory with the following:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">The final version of this UI code can be found in our <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/couchbase-examples\/netlify-tutorial\/blob\/main\/index.html\">sample application repo<\/a> if you want to copy and paste it in.<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n  &lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot; \/&gt;\n    &lt;title&gt;Couchbase with Netlify&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;h1&gt;Couchbase with Netlify Functions&lt;\/h1&gt;\n    &lt;div class=&quot;section&quot;&gt;\n      &lt;h2&gt;\n        Update &lt;code&gt;country&lt;\/code&gt; for Airline:\n        &lt;span class=&quot;highlight&quot;&gt;40 Mile Air&lt;\/span&gt;\n      &lt;\/h2&gt;\n      &lt;form onsubmit=&quot;handleCountrySubmit(event)&quot;&gt;\n        &lt;input\n          type=&quot;text&quot;\n          id=&quot;newCountry&quot;\n          placeholder=&quot;New Country for 40-Mile-Air&quot;\n          size=&quot;50&quot;\n        \/&gt;\n        &lt;button type=&quot;submit&quot;&gt;Submit&lt;\/button&gt;\n      &lt;\/form&gt;\n    &lt;\/div&gt;\n    &lt;div class=&quot;section&quot;&gt;\n      &lt;h2&gt;List of Airlines&lt;\/h2&gt;\n      &lt;ul id=&quot;airlines&quot;&gt;&lt;\/ul&gt;\n    &lt;\/div&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;\n\n&lt;script&gt;\n  async function handleCountrySubmit(e) {\n    e.preventDefault()\n\n    let country = e.target[0].value\n    await fetch(&#039;\/.netlify\/functions\/put_airline&#039;, {\n      method: &#039;PUT&#039;,\n      headers: {\n        &#039;Content-Type&#039;: &#039;application\/json&#039;,\n      },\n      body: JSON.stringify({ country: country }),\n    })\n\n    location.reload()\n  }\n\n  ;(async () =&gt; {\n    try {\n      let results = await fetch(&#039;\/.netlify\/functions\/get_airlines&#039;)\n      let parsedResults = await results.json()\n      parsedResults.rows.forEach((result) =&gt; {\n        const listItem = document.createElement(&#039;li&#039;)\n        listItem.innerText = result.airline.name\n        const subList = document.createElement(&#039;ul&#039;)\n        const subListItem = document.createElement(&#039;li&#039;)\n        const codeBlock = document.createElement(&#039;pre&#039;)\n        codeBlock.innerText = JSON.stringify(result.airline, null, 2)\n        subListItem.appendChild(codeBlock)\n        subList.appendChild(subListItem)\n        listItem.appendChild(subList)\n        document.getElementById(&#039;airlines&#039;).appendChild(listItem)\n      })\n    } catch (error) {\n      const listError = document.createElement(&#039;li&#039;)\n      const errorMsg = document.createElement(&#039;strong&#039;)\n      errorMsg.innerText = error.message\n      listError.innerText = `Error Fetching Airlines: `\n      listError.appendChild(errorMsg)\n      document.getElementById(&#039;airlines&#039;).appendChild(listError)\n    }\n  })()\n&lt;\/script&gt;\n\n&lt;style&gt;\n  body {\n    font-family: Arial, serif;\n  }\n  #airlines &gt; li &gt; ul {\n    list-style: none !important;\n  }\n  pre {\n    background: lightgray;\n    display: inline-block;\n    padding: 16px;\n    border-radius: 6px;\n  }\n  .highlight {\n    background: yellow;\n    padding: 6px 12px;\n    border-radius: 6px;\n  }\n  .section {\n    border: 2px solid darkgray;\n    border-radius: 6px;\n    margin: 24px 0;\n    padding: 0 0 20px 20px;\n    width: 90%;\n  }\n  form &gt; input,\n  button {\n    padding: 10px 16px;\n  }\n  button {\n    cursor: pointer;\n  }\n&lt;\/style&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Our project is now ready to deploy to Netlify. You can check your work by running <code>netlify dev<\/code> to run your project locally and navigating to the url shown in the terminal. Your web application should show some airline data and you should be able to modify the country of the 40-Mile-Air airline using the form.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"assets\/web_app.png\" alt=\"webapp\"><\/p>\n\n\n\n<h3 id=\"h-deploy-to-netlify\" class=\"wp-block-heading\">Deploy to Netlify<\/h3>\n\n\n\n<h4 id=\"h-github\" class=\"wp-block-heading\">Github<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If you haven&#8217;t already done so, <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.github.com\/en\/migrations\/importing-source-code\/using-the-command-line-to-import-source-code\/adding-locally-hosted-code-to-github\">create a new GitHub repository and push your code<\/a>.<\/p>\n\n\n\n<h4 id=\"h-netlify\" class=\"wp-block-heading\">Netlify<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Using the Netlify console, click <code>Import an Existing Project<\/code>, connect to your GitHub account, and select the repository that you&#8217;ve created. Check the Netlify docs for detailed steps to <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.netlify.com\/welcome\/add-new-site\/#import-from-an-existing-repository\">import a project<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"assets\/import_netlify_repo.png\" alt=\"import project\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Keep the defaults for the following site settings page and click <code>Deploy<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"assets\/netlify_import.png\" alt=\"site settings\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After the project is created, go to <code>Site Settings &gt; Environment variables<\/code>. Now use the Add a variable pull down menu and select Import from a .env file. Finally cut and paste in the contents of the .env file you created earlier and click the Import variables button.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"assets\/env_vars.png\" alt=\"env vars\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Alternatively, we can also use the Netlify CLI to <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.netlify.com\/configure-builds\/environment-variables\/#declare-variables\">import our environment variables<\/a> and <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.netlify.com\/site-deploys\/create-deploys\/\">deploy our project<\/a> using <code>netlify env:set VAR_NAME value<\/code> as well as <code>netlify deploy<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Any changes you make to the environment variables will require your site to be redeployed. After the site is finished deploying, navigate to the site. You should see some airline data; try using the text field to change the country of the 40-Mile-Air airline.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\"assets\/deploy.png\" alt=\"deploy\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are having issues, confirm that you have imported the correct environment variables into Netlify and the project is working locally. You can also check the reference sample application.<\/p>\n\n\n\n<h3 id=\"h-conclusion\" class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You have just created a full-stack application using Couchbase Capella and Netlify. We used Netlify Functions to create a REST API that reads and stores data in a Couchbase Capella database. Combined, they are powerful tools that allow developers to quickly build production-ready, scalable, complex applications without needing to worry about infrastructure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview The movement of static websites with serverless functions is gaining momentum due to their simplicity, performance, and painless deployments. With static websites, there is no need for a dedicated server to process dynamic content as all the pages are pre-built and served directly to the browser. This means that serverless functions can be used [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false,"inline_featured_image":false,"_acf":"","footnotes":""},"tutorial-languages":[653],"tutorial-tags":[546],"tutorial-technologies":[562,549],"tutorial-types":[555],"class_list":["post-10372","tutorials","type-tutorials","status-publish","hentry","tutorial-languages-nodejs","tutorial-tags-rest-api","tutorial-technologies-capella","tutorial-technologies-query","tutorial-types-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.8 (Yoast SEO v28.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Quickstart using Couchbase Capella and Netlify Serverless Functions - Couchbase<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/es\/developers\/tutorials\/quickstart-netlify\/\" \/>\n<meta property=\"og:locale\" content=\"es_MX\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Quickstart using Couchbase Capella and Netlify Serverless Functions\" \/>\n<meta property=\"og:description\" content=\"Overview The movement of static websites with serverless functions is gaining momentum due to their simplicity, performance, and painless deployments. With static websites, there is no need for a dedicated server to process dynamic content as all the pages are pre-built and served directly to the browser. This means that serverless functions can be used [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/es\/developers\/tutorials\/quickstart-netlify\/\" \/>\n<meta property=\"og:site_name\" content=\"Couchbase\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Couchbase\/\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/10\/Cloud-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2548\" \/>\n\t<meta property=\"og:image:height\" content=\"1171\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@couchbase\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"14 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/developers\\\/tutorials\\\/quickstart-netlify\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/developers\\\/tutorials\\\/quickstart-netlify\\\/\",\"name\":\"Quickstart using Couchbase Capella and Netlify Serverless Functions - Couchbase\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/#website\"},\"datePublished\":\"2023-06-23T08:43:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/developers\\\/tutorials\\\/quickstart-netlify\\\/#breadcrumb\"},\"inLanguage\":\"es\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.couchbase.com\\\/es\\\/developers\\\/tutorials\\\/quickstart-netlify\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/developers\\\/tutorials\\\/quickstart-netlify\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.couchbase.com\\\/ko\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Quickstart using Couchbase Capella and Netlify Serverless Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/#website\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/\",\"name\":\"Couchbase\",\"description\":\"The Operational Data Platform for AI\u2122\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/#organization\"},\"alternateName\":\"Couchbase, Inc.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"es\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/#organization\",\"name\":\"Couchbase\",\"alternateName\":\"Couchbase, Inc.\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/Couchbase_Logo_Couchbase_Token_Red-1.png\",\"contentUrl\":\"https:\\\/\\\/www.couchbase.com\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/Couchbase_Logo_Couchbase_Token_Red-1.png\",\"width\":1129,\"height\":1129,\"caption\":\"Couchbase\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/es\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Couchbase\\\/\",\"https:\\\/\\\/x.com\\\/couchbase\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/CouchbaseServer\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/couchbase\\\/\",\"https:\\\/\\\/github.com\\\/couchbase\\\/\",\"https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Couchbase,_Inc.\"],\"description\":\"Couchbase is a database software company headquartered in San Jose, California, providing an operational data platform for AI used by hundreds of global enterprises.\",\"legalName\":\"Couchbase, Inc.\",\"foundingDate\":\"2011-01-01\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Quickstart using Couchbase Capella and Netlify Serverless Functions - Couchbase","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/es\/developers\/tutorials\/quickstart-netlify\/","og_locale":"es_MX","og_type":"article","og_title":"Quickstart using Couchbase Capella and Netlify Serverless Functions","og_description":"Overview The movement of static websites with serverless functions is gaining momentum due to their simplicity, performance, and painless deployments. With static websites, there is no need for a dedicated server to process dynamic content as all the pages are pre-built and served directly to the browser. This means that serverless functions can be used [&hellip;]","og_url":"https:\/\/www.couchbase.com\/es\/developers\/tutorials\/quickstart-netlify\/","og_site_name":"Couchbase","article_publisher":"https:\/\/www.facebook.com\/Couchbase\/","og_image":[{"width":2548,"height":1171,"url":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/10\/Cloud-2.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@couchbase","twitter_misc":{"Est. reading time":"14 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/es\/developers\/tutorials\/quickstart-netlify\/","url":"https:\/\/www.couchbase.com\/es\/developers\/tutorials\/quickstart-netlify\/","name":"Quickstart using Couchbase Capella and Netlify Serverless Functions - Couchbase","isPartOf":{"@id":"https:\/\/www.couchbase.com\/es\/#website"},"datePublished":"2023-06-23T08:43:43+00:00","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/es\/developers\/tutorials\/quickstart-netlify\/#breadcrumb"},"inLanguage":"es","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/es\/developers\/tutorials\/quickstart-netlify\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/es\/developers\/tutorials\/quickstart-netlify\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/ko\/"},{"@type":"ListItem","position":2,"name":"Quickstart using Couchbase Capella and Netlify Serverless Functions"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/es\/#website","url":"https:\/\/www.couchbase.com\/es\/","name":"Couchbase","description":"The Operational Data Platform for AI\u2122","publisher":{"@id":"https:\/\/www.couchbase.com\/es\/#organization"},"alternateName":"Couchbase, Inc.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/es\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"es"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/es\/#organization","name":"Couchbase","alternateName":"Couchbase, Inc.","url":"https:\/\/www.couchbase.com\/es\/","logo":{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/www.couchbase.com\/es\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/11\/Couchbase_Logo_Couchbase_Token_Red-1.png","contentUrl":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/11\/Couchbase_Logo_Couchbase_Token_Red-1.png","width":1129,"height":1129,"caption":"Couchbase"},"image":{"@id":"https:\/\/www.couchbase.com\/es\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Couchbase\/","https:\/\/x.com\/couchbase","https:\/\/www.youtube.com\/c\/CouchbaseServer","https:\/\/www.linkedin.com\/company\/couchbase\/","https:\/\/github.com\/couchbase\/","https:\/\/en.wikipedia.org\/wiki\/Couchbase,_Inc."],"description":"Couchbase es una empresa de software de bases de datos con sede en San Jos\u00e9, California, que proporciona una plataforma de datos operativa para la inteligencia artificial utilizada por cientos de empresas globales.","legalName":"Couchbase, Inc.","foundingDate":"2011-01-01"}]}},"acf":[],"permalink_manager":null,"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/es\/wp-json\/wp\/v2\/tutorials\/10372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/es\/wp-json\/wp\/v2\/tutorials"}],"about":[{"href":"https:\/\/www.couchbase.com\/es\/wp-json\/wp\/v2\/types\/tutorials"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/es\/wp-json\/wp\/v2\/tutorials\/10372\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/es\/wp-json\/wp\/v2\/media?parent=10372"}],"wp:term":[{"taxonomy":"tutorial-languages","embeddable":true,"href":"https:\/\/www.couchbase.com\/es\/wp-json\/wp\/v2\/tutorial-languages?post=10372"},{"taxonomy":"tutorial-tags","embeddable":true,"href":"https:\/\/www.couchbase.com\/es\/wp-json\/wp\/v2\/tutorial-tags?post=10372"},{"taxonomy":"tutorial-technologies","embeddable":true,"href":"https:\/\/www.couchbase.com\/es\/wp-json\/wp\/v2\/tutorial-technologies?post=10372"},{"taxonomy":"tutorial-types","embeddable":true,"href":"https:\/\/www.couchbase.com\/es\/wp-json\/wp\/v2\/tutorial-types?post=10372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}