Invoking script in composition API

Hello,

what is the right approach to invoke a script that's stored in the database within the Composition API? Does the API Server has any similar method for invoking scripts as the App Server has?

Best regards

  • Hi Daniel,

    You have to wrap the script call inside an API endpoint. Here is the code for a sample endpoint.

        // This class shows how to expose a script call through the API.
        public class Scripts : IApiProvider
        {
            public void Build(IApiBuilder builder)
            {
                // This method takes two string parameters and returns a string.
                // For demonstration purposes, the method simply calls the script
                // VI_BuildInitials.
    
                builder.AddMethod(Method.Define("getinitials/{firstname}/{lastname}")
                    .WithParameter("firstname", typeof(string), isInQuery: false)
                    .WithParameter("lastname", typeof(string), isInQuery: false)
                    .HandleGet(qr =>
                    {
                        // Setup the script runner
                        var scriptClass = qr.Session.Scripts().GetScriptClass(ScriptContext.Scripts);
                        var runner = new ScriptRunner(scriptClass, qr.Session);
    
                        // Add any script input parameters to this array.
                        // In this example, the script parameters are defined as
                        // URL parameters, and their values must be supplied
                        // by the client. This does not have to be the case.
                        var parameters = new object[]
                        {
                            qr.Parameters.Get<string>("firstname"),
                            qr.Parameters.Get<string>("lastname")
                        };
    
                        // This assumes that the script returns a string.
                        return runner.Eval("VI_BuildInitials", parameters) as string;
                    }));
            }
        }