Examples

SuiteScript REST API

Building a REST API

SuiteScript REST API with RESTlets handles JSON CRUD operations.

Introduction to SuiteScript REST API

The SuiteScript REST API allows developers to build robust integrations with NetSuite by handling JSON CRUD (Create, Read, Update, Delete) operations through RESTlets. RESTlets are server-side scripts that interact with NetSuite's data layer, providing a flexible and powerful interface for external applications.

This guide will walk you through the basics of creating and using RESTlets to perform CRUD operations. It will cover setting up your environment, creating RESTlets, and handling requests with JSON data.

Setting Up Your Environment

Before you begin, ensure that you have access to a NetSuite account with the proper permissions to create and deploy scripts. You'll also need SuiteCloud IDE or a similar development environment to write and manage your scripts.

Follow these steps to set up your environment:

  • Log in to your NetSuite account and navigate to the 'SuiteCloud Development Framework'.
  • Ensure "RESTlet" is enabled under SuiteScript features.
  • Set up a new SuiteScript file in your chosen IDE.

Creating a RESTlet Script

A RESTlet script in SuiteScript 2.0 consists of several parts, including the definition of entry points for GET, POST, PUT, and DELETE requests. Below is a basic example of a RESTlet script:

Handling JSON Requests and Responses

When interacting with the SuiteScript REST API, requests and responses are typically formatted in JSON. This format is both easy to read and widely supported across various programming environments.

In the example above, the doGet function retrieves a customer record based on an ID passed in the request parameters, while the doPost function creates a new customer record using the data provided in the request body.

Deploying Your RESTlet

Once your RESTlet script is written, you'll need to deploy it within your NetSuite account. This involves creating a Script Deployment in NetSuite:

  • Navigate to Customization > Scripting > Script Deployments > New.
  • Select your script file and configure the deployment settings, such as the audience and status.
  • Save the deployment and note the external URL provided, which will be used to access your RESTlet.

Testing Your RESTlet

Testing is a crucial step to ensure your RESTlet works as expected. Use tools like Postman or curl to send HTTP requests to the RESTlet's URL. Make sure to test all CRUD operations (GET, POST, PUT, DELETE) and verify the responses.

For example, use the following curl command to test the GET operation:

Conclusion

The SuiteScript REST API with RESTlets provides a powerful mechanism for integrating external systems with NetSuite. By setting up RESTlets to handle JSON CRUD operations, you can efficiently manage data and automate processes within your NetSuite environment.

Continue exploring more advanced topics such as error handling, performance optimization, and security best practices to enhance your RESTlet implementations.