RedwoodJS

v0.26.2

# Serverless Functions

Work in Progress ⚠️

There's more to document here. In the meantime, you can check our community forum for answers.

Want to contribute? Redwood welcomes contributions and loves helping people become contributors. You can edit this doc here. If you have any questions, just ask for help! We're active on the forums and on discord.

Redwood looks for serverless functions in api/src/functions. Each function is mapped to a URI based on its filename. For example, you can find api/src/functions/graphql.js at http://localhost:8911/graphql.

# Creating Serverless Functions

Creating serverless functions is easy with Redwood's function generator:

yarn rw g function <name>

It'll give you a stub that exports a handler that returns a status code—the bare minimum you need to get going:

export const handler = async (event, context) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      data: '${name} function',
    }),
  }
}

# The handler

For a lambda function to be a lambda function, it must export a handler that returns a status code. The handler receives two arguments: event and context. Whatever it returns is the response, which should include a statusCode at the very least.

Note that you can use code in api/src in your serverless function, such as importing the db from src/lib/db.

# Developing locally

When you're developing locally, the dev server watches the api directory for modifications; when it detects any, it reimports all the modules.