Skip to main content

api

Overview of the api module in the @serverless-stack/node package.

import { ... } from "@serverless-stack/node/api"

The api module has the following exports.


Properties

The properties let you access the resources that are bound to the function.


Api

This module helps with accessing Api constructs.

import { Api } from "@serverless-stack/node/api";

url

Type : string

The URL of the API. If custom domain is enabled, this is the custom domain URL of the API.

console.log(Api.myApi.url);

GraphQLApi

This module helps with accessing GraphqlApis.

import { GraphQLApi } from "@serverless-stack/node/api";
console.log(GraphQLApi.myApi.url);

url

Type : string

The URL of the API. If custom domain is enabled, this is the custom domain URL of the API.


AppSyncApi

This module helps with accessing AppSyncApis.

import { AppSyncApi } from "@serverless-stack/node/api";
console.log(AppSyncApi.myApi.url);

url

Type : string

The URL of the API. If custom domain is enabled, this is the custom domain URL of the API.


WebSocketApi

This module helps with accessing WebSocketApis.

import { WebSocketApi } from "@serverless-stack/node/api";
console.log(WebSocketApi.myApi.url);

url

Type : string

The URL of the API. If custom domain is enabled, this is the custom domain URL of the API.


ApiGatewayV1Api

This module helps with accessing ApiGatewayV1Apis.

import { ApiGatewayV1Api } from "@serverless-stack/node/api";
console.log(ApiGatewayV1Api.myApi.url);

url

Type : string

The URL of the API. If custom domain is enabled, this is the custom domain URL of the API.


Handlers

The handlers can wrap around your Lambda function handler.


ApiHandler

The ApiHandler provides a function that can be used to implement the API handler function.

import { useBody, ApiHandler } from "@serverless-stack/node/api";

export const handler = ApiHandler((event) => {
const body = useBody();

// ...
});

Hooks

The hooks are functions that have access to the current invocation.


useBody

This hook returns the request body.

import { useBody } from "@serverless-stack/node/api";
const body = useBody();

useJsonBody

This hook returns the request body in JSON decoded format.

import { useJsonBody } from "@serverless-stack/node/api";
const json = useJsonBody();

useCookie

This hook returns a request cookie.

import { useCookie } from "@serverless-stack/node/api";
const cookie = useCookie("token");

useCookies

This hook returns all request cookies.

import { useCookies } from "@serverless-stack/node/api";
const cookies = useCookies();

useHeader

This hook returns a request header.

import { useHeader } from "@serverless-stack/node/api";
const header = useHeader("Authorization");

useHeaders

This hook returns all request headers.

import { useHeaders } from "@serverless-stack/node/api";
const headers = useHeaders();

useFormData

This hook returns the request form data.

import { useFormData } from "@serverless-stack/node/api";
const data = useFormData();

useFormValue

This hook returns the request form value.

import { useFormValue } from "@serverless-stack/node/api";
const name = useFormValue("name");

useQueryParam

This hook returns a request query parameter.

import { useQueryParam } from "@serverless-stack/node/api";
const name = useQueryParam("name");

useQueryParams

This hook returns all request query parameters.

import { useQueryParams } from "@serverless-stack/node/api";
const params = useQueryParams();