Use nhost.functions.call to call (sending a POST request to) a serverless function. Use generic types to specify the expected response data, request body and error message.

await nhost.functions.call('send-welcome-email', {
  email: 'joe@example.com',
  name: 'Joe Doe'
})

Parameters


url required string


body optional null | TBody


config optional NhostFunctionCallConfig


Examples

Without generic types

await nhost.functions.call('send-welcome-email', {
  email: 'joe@example.com',
  name: 'Joe Doe'
})

Using generic types

type Data = {
  message: string
}

type Body = {
  email: string
  name: string
}

type ErrorMessage = {
  details: string
}

// The function will only accept a body of type `Body`
const { res, error } = await nhost.functions.call<Data, Body, ErrorMessage>(
  'send-welcome-email',
  { email: 'joe@example.com', name: 'Joe Doe' }
)

// Now the response data is typed as `Data`
console.log(res?.data.message)

// Now the error message is typed as `ErrorMessage`
console.log(error?.message.details)