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
modal.functions.fromName(appName, name, params?)
Reference a Function by its name in an App.
Name of the App containing the Function.
Name of the Function to reference.
Optional parameters If true, creates the Function if it doesn’t exist. Defaults to false.
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
Unique identifier for the Function.
Method name if this Function is a class method.
Methods
remote(args?, kwargs?)
Execute the Function remotely and wait for the result.
Positional arguments to pass to the Function. Defaults to [].
Keyword arguments to pass to the Function. Defaults to {}.
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.
Positional arguments to pass to the Function.
Keyword arguments to pass to the Function.
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.
Statistics object containing: Number of queued inputs waiting to be processed.
Total number of running containers for this Function.
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 Minimum number of containers to maintain.
Maximum number of containers to scale to.
Number of extra containers to maintain for burst capacity.
Time in milliseconds before scaling down idle containers. Must be a multiple of 1000.
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 );