Not to be confused with React Server
Functions; while they serve similar purposes,
Attio server functions have different requirements and therefore do not follow the RSC protocol. .server.ts suffix. The file must export a default async function, which will serve as the
entry point for the server function. You can then import and call this function within the client
code.
Here is an example of using a server function to make
a request
to Outreach on behalf of a user.
// add-to-sequence.server.ts
import {getUserConnection} from "attio/server"
export default async function addToSequence({
  prospectId,
  sequenceId,
  mailboxId,
}: {
  prospectId: string
  sequenceId: number
  mailboxId: number
}): Promise<{id: string}> {
  const response = await fetch(`https://api.outreach.io/api/v2/sequenceStates`, {
    method: "POST",
    headers: {
      "Content-Type": "application/vnd.api+json",
      "Authorization": `Bearer ${getUserConnection().value}`,
    },
    body: JSON.stringify({prospectId, sequenceId, mailboxId}),
  })
  if (!response.ok) {
    throw new Error(`Failed to add prospect to sequence: ${await response.text()}`)
  }
  // ℹ️ better to parse with a tool like Zod than to cast
  const body = (await response.json()) as {data: Record<string, string>}
  return body.data
}
addToSequence() function can be called from your client side code like:
import addToSequence from "add-to-sequence.server.ts"
...
const sequenceState = await addToSequence({
  prospectId: "XXX",
  sequenceId: 42,
  mailboxId: 42
})
Whatever you pass to or return from a server function will go through
JSON.stringify(JSON.parse(...)).
try/catch or .catch
promise chaining.
Debugging
To check output of console.log() inside the server functions, open the Logs tab for your app in the Developer Portal.
Timeout
Server functions have a non-configurable 30 second timeout. If a server function takes longer than 30 seconds to complete, it will be terminated and a timeout error will be thrown.