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 interact with NetSuite data using JSON for CRUD operations. RESTlets, a component of the SuiteScript API, provide a flexible way to integrate NetSuite with external systems. They are ideal for building custom RESTful web services in NetSuite.

Let's explore how RESTlets function and how you can use them to perform CRUD operations.

Creating a RESTlet

To create a RESTlet, you'll need to define it in SuiteScript and deploy it within NetSuite. RESTlets are written in JavaScript and typically defined using SuiteScript 2.0. Below is a basic example of a RESTlet script:

In this example, the RESTlet script is designed to retrieve a customer record using the GET request. The record.load method is used to load a specific customer record by its ID.

Next, you need to deploy this script in NetSuite, which involves setting up the deployment record and specifying the URL where the RESTlet will be accessible.

Performing CRUD Operations

Once your RESTlet is deployed, it can handle various HTTP methods to perform CRUD operations. Here's a quick overview of how to use RESTlets for each operation:

  • Create: Use an HTTP POST request to create new records.
  • Read: Use an HTTP GET request to retrieve records.
  • Update: Use an HTTP PUT request to update existing records.
  • Delete: Use an HTTP DELETE request to remove records.

Example: Creating a New Record

Here's an example of how you can create a new customer record using a POST request:

In this POST request example, we create a new customer record by setting values for the firstname and lastname fields. The record is then saved, and the new customer's ID is returned.

Handling Errors in RESTlets

When working with RESTlets, it's crucial to handle errors effectively. You can use try-catch blocks in your SuiteScript to manage exceptions and return meaningful error messages to the client. This ensures robust and user-friendly API behavior.

The above code snippet demonstrates error handling in a POST method by using the try-catch statement. This approach helps you capture and respond to errors gracefully.