Skip to main content

Developing Functions

PhotonIQ Functions allow you to implement system logic with ready-to-use code blocks of code called "functions." These functions provide the resources needed to keep your applications running smoothly, allowing you to focus on writing less code and managing fewer infrastructure components—leading to significant cost savings.

Create a new function

Before you begin, set up the functions CLI on your server. To create a new function, use the following command:

faas new <function_name>

By default, functions are created in Rust.

tip

PhotonIQ Functions currently support building source code in Next.js, Rust, and JavaScript environments. Select your preferred language/framework below to to learn how to develop PhotonIQ functions.

To create a Next.js function, run this command:

faas new <function_name> --lang nextjs

This command scaffolds a new Next.js app in the functions/<function_name> directory. It also generates a photoniq-faas-sdk directory.

The Next.js app contains a pre-defined template function code for Javascript and Typescript in /src/app/api/pingjs/route.js and /src/app/api/pingts/route.ts respectively. Visit the quickstart guide to test the result of this default function.

PhotonIQ Functions support Typescript and Javascript functions using Next.js. You can define a function in a file under /src/app/api/{example}/route.js.

To create Next.js functions, you can:

  • Export a function that acts as an handler.
api/{example}/route.js
export function GET(req) {
return new Response('[GET] PhotonIQ FaaS function is working. Hello, PhotonIQ Next.js Functions is working', {
status: 200,
headers: { 'Content-Type': 'text/plain' },
});
}
  • Use Route Handlers to create custom request handlers for a given route.
api/{example}/route.ts
export function GET(request: Request) {
return new Response('[GET] PhotonIQ FaaS function is working for Next.js.', {
status: 200,
headers: { 'Content-Type': 'text/plain' },
});
}

After defining the function, use the faas build <function_name> and faas run <function_name> command to build and run the function on a local server respectively. For more advanced functions, visit Creating HTTP requests using functions.

The photoniq.toml file contains the function configuration settings. Refer to Configuring Functions for more details.