The node.script JavaScript API / max-api Module

You can communicate and interact bi-directionally with Max and scripts running in the node.script object using the max-api module. The max-api module contains functions for sending messages to - and receiving messages from - Max.

You can find a complete listing and description of those functions here: https://docs.cycling74.com/nodeformax/api/

You can also open the API documentation by sending the api message to a node.script object:

Loading the API

When you create a JavaScript file for use with Node for Max, the first thing you’ll do is require “max-api”:

const maxApi = require(“max-api”);

Note: While the max-api module resembles a Node module, you don’t need to send a message like npm install max-api to your node.script object to get things started; when you run a JavaScript file using the node.script object, Max will automatically include the code from the “max-api” module.

Sometimes you want to know if a script is running from within Max, Max for Live, or a Max Standalone application. For this, node.script adds an environment variable called MAX_ENV that you can access as follows:

if (process.env.MAX_ENV === “max”) {
    console.log(“running in Max”);
    } else if (process.env.MAX_ENV === “maxforlive”) {
    	console.log(“running in Max For Live”);
    } else if (process.env.MAX_ENV === “max:standalone”) {
    	console.log(“running in a Max Standalone application”)
    } else {
    	console.log(“the program is not running within a node.script”);
    }

You can find simple code examples that demonstrate the functionality of the max-api at https://github.com/Cycling74/n4m-core-examples

API Functions by category

Here is a list of API functions available:

Handlers

addHandler Call a function when node.script receives a Max message.
addHandlers Create multiple handlers at once.
removeHandler Remove the handler for a given Max message.
removeHandlers Remove multiple handlers at once. Call with no arguments to remove all handlers.

Dictionaries

getDict Retrieve the contents of a named Max dictionary.
setDict Overwrite the entire contents of a named Max dictionary.
updateDict Update the value for a given key in a named Max dictionary.

Output

outlet Send anything out of the leftmost outlet of the node.script object. JavaScript objects will be transformed into Max dictionaries.
outletBang Send a bang out of the leftmost outlet of the node.script object.

Posting

post Print a message to the Max console.

See Also

Name Description
Node For Max Documentation - Table of Contents Node For Max Documentation - Table of Contents
node.script