Calling Magento 2 API from NetSuite

I needed a way to move data from NetSuite to Magento. Here is a simple example that creates an attribute set in Magento. This example runs in a NetSuite debugger session. It is written in SuiteScript 2.1.

To create your token in Magento. Login in as admin. Go to
System >> Integrations >> Add New Integration

Once you’ve added your integration, Magento will create an Access Token. This is your Bearer Token.

Use the Bearer Token in your header. Refer to the Magento 2 Admin REST endpoint’s documentation to correctly build the body. Here is example code.

require(['N/https'],
    (https) => {

        var header = {
            'Authorization': 'Bearer [your token here]',
            'Accept': 'application/json',
            'Content-Type': 'application/json; charset=utf-8'
        };

        var body = JSON.stringify(
            {
                "attributeSet": {
                    "attribute_set_name": "[Your attribute set name here]"
                },
                "skeletonId": 4
            }
        );

        https.post.promise({
            url: encodeURI('https://[Your Magento base domain here]/rest/default/V1/products/attribute-sets'),
            body: body,
            headers: header
        })
            .then(function (response) {
                log.debug({
                    title: 'Response',
                    details: response
                });
            })
            .catch(function onRejected(reason) {
                log.debug({
                    title: 'Invalid Request: ',
                    details: reason
                });
            })


    }
)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s