Skip to main content
Functions represent deployed Modal functions that can be invoked remotely from your TypeScript application.

Access the Functions API

import { ModalClient } from "modal";

const modal = new ModalClient();
const fn = await modal.functions.fromName("my-app", "my_function");

Methods

Reference a Function by its name in an App.
appName
string
required
Name of the App containing the Function.
name
string
required
Name of the Function to reference.
params
FunctionFromNameParams
Optional parameters
return
Promise<Function_>
The referenced Function object.
const fn = await modal.functions.fromName("my-app", "square");
To call class methods, use the Cls API instead.

Function object

Properties

functionId
string
Unique identifier for the Function.
methodName
string | undefined
Method name if this Function is a class method.

Methods

remote(args?, kwargs?)

Execute the Function remotely and wait for the result.
args
any[]
Positional arguments to pass to the Function. Defaults to [].
kwargs
Record<string, any>
Keyword arguments to pass to the Function. Defaults to {}.
return
Promise<any>
The return value from the Function.
const fn = await modal.functions.fromName("my-app", "square");

// Call with positional arguments
const result = await fn.remote([5]);
console.log(result); // 25

// Call with keyword arguments
const greet = await modal.functions.fromName("my-app", "greet");
const message = await greet.remote([], { name: "Alice" });
console.log(message); // "Hello, Alice!"
Requires the deployed app to use Modal Python SDK 1.2 or newer for serialization compatibility.

spawn(args?, kwargs?)

Spawn the Function asynchronously without waiting for the result.
args
any[]
Positional arguments to pass to the Function.
kwargs
Record<string, any>
Keyword arguments to pass to the Function.
return
Promise<FunctionCall>
A FunctionCall object that can be used to retrieve the result later.
const fn = await modal.functions.fromName("my-app", "process_data");

// Spawn the function
const call = await fn.spawn(["large_dataset"]);

// Do other work...

// Get the result later
const result = await call.get();

getCurrentStats()

Get current statistics about the running Function.
return
Promise<FunctionStats>
Statistics object containing:
const stats = await fn.getCurrentStats();
console.log(`Backlog: ${stats.backlog}, Runners: ${stats.numTotalRunners}`);

updateAutoscaler(params)

Override the autoscaling behavior for this Function.
params
FunctionUpdateAutoscalerParams
required
Autoscaler configuration
await fn.updateAutoscaler({
  minContainers: 1,
  maxContainers: 10,
  bufferContainers: 2,
  scaledownWindowMs: 60000, // 60 seconds
});

getWebUrl()

Get the web URL if this Function is deployed as a web endpoint.
return
Promise<string | undefined>
The web URL, or undefined if this is not a web endpoint.
const url = await fn.getWebUrl();
if (url) {
  console.log(`Function available at: ${url}`);
}

Example: Batch processing

import { ModalClient } from "modal";

const modal = new ModalClient();
const processImage = await modal.functions.fromName("my-app", "process_image");

const images = ["img1.jpg", "img2.jpg", "img3.jpg"];

// Spawn all calls asynchronously
const calls = await Promise.all(
  images.map((img) => processImage.spawn([img]))
);

// Wait for all results
const results = await Promise.all(
  calls.map((call) => call.get())
);

console.log("Processed images:", results);

Build docs developers (and LLMs) love