Integrating RapidAPI with NetSuite SuiteScript

I am experiencing difficulties while trying to utilize RapidAPI within NetSuite. Has anyone successfully accomplished this? Here’s an example of my setup:

const headers = [
    {
        name: "x-rapidapi-host",
        value: "your_host"
    },
    {
        name: "x-rapidapi-key",
        value: "your_key"
    }
];

const apiResponse = https.request({
    method: https.Method.GET,
    url: "your_api_url",
    headers: headers
});

The response I receive is:

error 400 message: Invalid API key. For further information, visit the documentation.

I verified that the API key works correctly in Postman.

Hey Alex, sounds frustrating! If your API key works in Postman but not in SuiteScript, make sure your headers are set correctly. Here's a compacted version to try:

const headers = {};
headers["x-rapidapi-host"] = "your_host";
headers["x-rapidapi-key"] = "your_key";

const apiResponse = https.request({
    method: https.Method.GET,
    url: "your_api_url",
    headers: headers
});

Check for any typo in "your_api_url", and ensure the keys match the API documentation. Hope this helps!

Hi Alex, integrating RapidAPI with NetSuite can be tricky but manageable. Let's streamline the approach:

1. Verify Authorization: Double-check that your API key is authorized for the endpoint you're attempting to access. Confirm that all permissions are correctly set in RapidAPI.

2. Simplify Header Construction: Ensure headers are set as a JavaScript object directly:

const headers = {
    'x-rapidapi-host': 'your_host',
    'x-rapidapi-key': 'your_key'
};

const apiResponse = https.request({
    method: https.Method.GET,
    url: 'your_api_url',
    headers: headers
});

3. Debugging: Use logging to check if headers are correctly passed and what the exact API response is:

log.debug('Full API Response', apiResponse);

4. Endpoints and Restrictions: Review any endpoint restrictions or cross-origin requirements imposed by RapidAPI.

Following these checks should help resolve the issue. If the problem persists, consider reaching out to the API provider for further guidance. Best of luck!

Alex, ensuring the correct header structure is indeed crucial, but there are additional considerations in using RapidAPI with NetSuite SuiteScript:

Validate API Key Authorization: Make sure that your API key is properly authorized for the specific API you are trying to access. Sometimes, access rights need to be explicitly granted for API keys.

Header Object Construction: In SuiteScript, headers need to be constructed as a JavaScript object, similar to JSON format. Here's a revised approach:

const headers = {
    'x-rapidapi-host': 'your_host',
    'x-rapidapi-key': 'your_key'
};

const apiResponse = https.request({
    method: https.Method.GET,
    url: 'your_api_url',
    headers: headers
});

Further Debugging: Implementing logging can help identify if headers are correctly passed. You can log the `apiResponse` to analyze the full error message or status code detail:

log.debug('API Response', apiResponse);

Consider CORS and Endpoint Restrictions: RapidAPI might have certain restrictions in place. Double-check if there are specific requirements around origin or endpoint restrictions.

Following these steps should narrow down the cause of error 400. Let me know how it goes!