{"id":19201083,"url":"https://github.com/dabit3/basic-serverless-api","last_synced_at":"2025-11-17T15:26:20.090Z","repository":{"id":50869970,"uuid":"221790039","full_name":"dabit3/basic-serverless-api","owner":"dabit3","description":"A basic full stack example of building an API with AWS Amplify, Amazon API Gateway, AWS Lambda, and Amazon DynamoDB","archived":false,"fork":false,"pushed_at":"2021-06-08T07:27:51.000Z","size":2293,"stargazers_count":46,"open_issues_count":11,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-05T22:54:12.396Z","etag":null,"topics":["aws","aws-amplify","dynamodb","lambda","serverless"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dabit3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-14T21:29:47.000Z","updated_at":"2025-01-05T08:42:05.000Z","dependencies_parsed_at":"2022-09-04T00:53:12.554Z","dependency_job_id":null,"html_url":"https://github.com/dabit3/basic-serverless-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabit3%2Fbasic-serverless-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabit3%2Fbasic-serverless-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabit3%2Fbasic-serverless-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabit3%2Fbasic-serverless-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dabit3","download_url":"https://codeload.github.com/dabit3/basic-serverless-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253742082,"owners_count":21956952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-amplify","dynamodb","lambda","serverless"],"created_at":"2024-11-09T12:36:27.932Z","updated_at":"2025-11-17T15:26:15.053Z","avatar_url":"https://github.com/dabit3.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Basic serverless API\n\nBuilding a basic serverless API with DynamoDB, API Gateway, and AWS Lambda\n\n## Getting started\n\nCreate the React project, install dependencies\n\n```sh\n$ npx create-react-app serverless-api\n\n$ cd serverless-api\n\n$ npm install aws-amplify\n```\n\nNext, create the amplify project and add the database:\n\n```sh\n$ amplify init\n\n$ amplify add storage\n\n? Please select from one of the below mentioned services: NoSQL Database\n```\n\nCreate a database with the following columns:\n\n```\nid: string\nname: string\ndescription: string\nprice: number\n\n? Please choose partition key for the table: id\n? Do you want to add a sort key to your table? Y\n? Please choose sort key for the table: name\n? Do you want to add global secondary indexes to your table? N\n? Do you want to add a Lambda Trigger for your Table? N\n```\n\nNext, create the API:\n\n```sh\n$ amplify add api\n\n? Please select from one of the below mentioned services: REST\n? Provide a friendly name for your resource to be used as a label for this category in the project: productapi\n? Provide a path (e.g., /items) /products\n? Choose a Lambda source: Create a new Lambda function\n? Provide a friendly name for your resource to be used as a label for this category in the project: \u003cfunction_name\u003e\n? Provide the AWS Lambda function name: \u003cfunction_name\u003e\n? Choose the function template that you want to use: Serverless express function (Integration with Amazon API Gateway)\n? Do you want to access other resources created in this project from your Lambda function? Yes\n? Select the category: storage\nStorage category has a resource called \u003ctable_name\u003e\n? Select the operations you want to permit for \u003ctable_name\u003e create, read, update, delete\n? Do you want to edit the local lambda function now? Yes\nFile will open in your editor. We will edit this file in the next step, but for now press enter to continue.\n? Press enter to continue\n? Would you like to restrict API access? N\n? Would you like to add another path? N\n```\n\nNext, update the function with the following changes:\n\n```javascript\n/* amplify/backend/function/\u003cfunction_name\u003e/src/app.js */\n\n/* region and table name available in comments of lambda function */\nconst region = process.env.REGION\nconst ddb_table_name = process.env.\u003cYOUR_STORAGE_NAME\u003e\n\nconst AWS = require('aws-sdk')\nconst uuid = require('uuid/v4')\nconst docClient = new AWS.DynamoDB.DocumentClient({region})\n\n// update the /products \"get\" and \"post\" endpoints\n \napp.get('/products', async function(req, res) {\n  try {\n    var params = {\n      TableName: ddb_table_name,\n    }\n    const data = await docClient.scan(params).promise()\n    res.json({\n      data: data\n    })\n  } catch (err) {\n    res.json({\n      error: err\n    })\n  }\n})\n\napp.post('/products', async function(req, res) {\n  const { body } = req\n  try {\n    const input = { ...body, id: uuid() }\n    var params = {\n      TableName: ddb_table_name,\n      Item: input\n    }\n    await docClient.put(params).promise()\n    res.json({\n      success: 'item saved to database..'\n    })\n  } catch (err) {\n    res.json({\n      error: err\n    })\n  }\n})\n```\n\nNext, update the dependencies in the lambda function to include __uuid__:\n\n__amplify/backend/function/\u003cfunction_name\u003e/src/package.json__\n\n```json\n\"dependencies\": {\n  \"aws-serverless-express\": \"^3.3.5\",\n  \"body-parser\": \"^1.17.1\",\n  \"express\": \"^4.15.2\",\n  \"uuid\": \"^3.3.3\"\n},\n```\n\nNext, deploy the back end:\n\n```sh\n$ amplify push\n```\n\n### Client-side code\n\nNext, open __src/index.js__ and add the following:\n\n```javascript\nimport Amplify from 'aws-amplify'\nimport config from './aws-exports'\nAmplify.configure(config)\n```\n\nNext, open __src/App.js__ and add the following code:\n\n```javascript\nimport React, { useEffect, useState } from 'react';\nimport logo from './logo.svg';\nimport './App.css';\nimport { API } from 'aws-amplify'\n\nconst initialState = {\n  name: '',\n  description: '',\n  price: ''\n}\n\nfunction App() {\n  const [products, setProducts] = useState([])\n  const [product, updateProduct] = useState(initialState)\n  async function fetchProducts() {\n    const products = await API.get('productapi', '/products')\n    setProducts(products.data.Items)\n  }\n  async function createProduct() {\n    const { name, description, price } = product\n    if (!name || !description || !price) return\n    const data = {\n      body: { ...product, price: parseInt(product.price) }\n    }\n    await API.post('productapi', '/products', data)\n    console.log('product successfully created...')\n    updateProduct(initialState)\n    fetchProducts()\n  }\n  const updateProductInput = key =\u003e event =\u003e {\n    updateProduct({ ...product, [key]: event.target.value })\n  }\n  useEffect(() =\u003e {\n    fetchProducts()\n  }, [])\n  return (\n    \u003cdiv className=\"App\"\u003e\n      {\n        products.map((product, index) =\u003e (\n          \u003cdiv key={index}\u003e\n            \u003ch3\u003e{product.name}\u003c/h3\u003e\n            \u003cp\u003e{product.description}\u003c/p\u003e\n            \u003ch4\u003e${product.price}\u003c/h4\u003e\n          \u003c/div\u003e\n        ))\n      }\n      \u003cdiv style={form}\u003e\n        \u003cinput\n          placeholder=\"Product name\"\n          value={product.name}\n          onChange={updateProductInput(\"name\")}\n          style={input}\n        /\u003e\n        \u003cinput\n          placeholder=\"Product description\"\n          value={product.description}\n          onChange={updateProductInput(\"description\")}\n          style={input}\n        /\u003e\n        \u003cinput\n          placeholder=\"Product price\"\n          value={product.price}\n          onChange={updateProductInput(\"price\")}\n          style={input}\n        /\u003e\n        \u003cbutton style={button} onClick={createProduct}\u003eCreate Product\u003c/button\u003e\n      \u003c/div\u003e\n    \u003c/div\u003e\n  );\n}\n\nconst button = {\n  padding: '10px 40px',\n  width: 400,\n  margin:  '0px auto'\n}\n\nconst input = {\n  padding: 7,\n  width: 400,\n  margin: '0px auto 6px'\n}\n\nconst form = {\n  display: 'flex', flexDirection: 'column', padding: 60\n}\n\nexport default App;\n```\n\n### Test everything out\n\n```sh\n$ npm start\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdabit3%2Fbasic-serverless-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdabit3%2Fbasic-serverless-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdabit3%2Fbasic-serverless-api/lists"}