NodeJS sample

A complete sample app written in NodeJS to access OSAIS services

1/ Authenticate into OSAIS as a Client App

Your application needs to be authenticated into OSAIS. Our authToken has a validity of 3 days, but we recommend authenticating into OSAIS every 24 hours.

const async_login = async function (){
    try {
        let response = await axios.post("https://opensourceais.com/api/v1/public/client/login", {
            token: <your_token>,
            secret: <your_secret>
        }, {
            headers: { 
                name: 'content-type',
                value: 'application/json'
            }
        });
        if(response && response.data) {
            // response.data contains the authentication token as {authToken: ...}
            // user the authToken in Bearer of next calls
            return response.data;             
        }

        throw {
            data: null,
            status: 400,
            statusText: "Could not authenticate as a client"
        }
    }
    catch (err) {
        throw err;
    }
}

2/ Implement routes for receiving notifications from OSAIS

You application needs to implement two routes for receiving notifications from the AI. One route is for the stage notification, and one route is for the file upload notification and processing.

3/ Send an AI request to OSAIS

Your application can call AIs by sending a POST request to OSAIS. Each AI has its own route into OSAIS, which is simple api/v1/private/client/ai/<name_of_the_ai>. Here below are two examples, one for AI Ping, one for AI Stable Diffusion.

4/ Understand what to process on the Notification calls

Notifications are received on the /notify route. Here are the various possible stages that can be received. Note that images can still be received after the AI has stopped processing. It is also possible that several images are received, one per notification, if the AI was requested to output multiple files.

AI generated files (in the case of Stable Diffusion, images) are received on the /upload route. Note that in this case, via our service "imgServices.uploadImg", we used multer to process the image immediately. here is the code for this.

It is important to return correct calls, since for example, in the case of a badly configured nGROK (calling an unknown endpoint), the notification endpoint would still return (no error) but it would not be a valid acknowledgement that the request was processed correctly by the receiving application.

The expected returned data is a properly formed JSON as below

Both the <token> and the <uid> are passed in parameters to the notification endpoint, so it is just a matter for the receiving application to send them back as acknowledgement of having processed the notification.

Last updated