Upload source maps via API

New Relic Browser supports the uploading of source maps, which are used to de-minify error stack traces on the JS errors page. This document will explain how to publish/upload source maps to Browser via the API.

Access to this feature depends on your subscription level.

For general information about the source maps functionality, and how to upload source maps via the UI, see New Relic Browser source maps. For information on generating source maps, see Generate source maps.

Prepare for using the source map API

In order to upload source maps to New Relic Browser via the API, you'll need this information:

Below are some questions to help you understand how to acquire this required information:

What is the JavaScript URL?

Every time the agent captures an error in your code, it's associated with the URL of the JavaScript in which it occurred. This is the src attribute of the script tag in your HTML. This full JavaScript URL is required when sending source maps to Browser.

You can find the URL for an error's JavaScript file in New Relic Browser, on the JS errors page. See New Relic Browser source maps for more on finding these errors in the UI.

Is a release name and ID required?

Many organizations include a version number or hash in the JavaScript URL. This is generally added to "bust" caches to ensure your users get the most recent version of your code. This type of URL might look something like:

  • https://example.com/assets/application-59.min.js
  • https://example.com/assets/bundle-d6d031.min.js
  • https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js

If your app's URLs automatically have the version info appended to it, the Browser agent has everything it needs in order to match errors with your code. You can move ahead to generating source maps.

If this doesn't apply to you, and JS URLs do not have version info appended, you’ll have to assist the agent by specifying a release name and ID with the API.

Are there limits to source map uploads?

There is no limit to the overall number of source maps you can upload. However, the API is rate-limited:

  • You can upload a maximum of 100 source maps per minute
  • You can upload a maximum of 5,000 source maps per day

Source map files can be a maximum of 50Mb in size.

Push source maps to New Relic

Now that you have a source map (or maps), you are ready to publish it to New Relic Browser. There are two recommended ways to send source maps to Browser via the API:

  • Using the New Relic npm module: you can upload source maps via the command line or via a client-side JavaScript build/deploy script like Gulp or Grunt.
  • Using cURL.

These two methods are described in more detail below.

Also note that you can upload source maps via the Browser UI.

Use npm module via command line or client-side script

The easiest and recommended way to upload source maps to Browser is to use the our new @newrelic/publish-sourcemap npm module. It provides a command line tool and Javascript API to accomplish this task. More documentation is available in the npm repo.

Here are some examples of using the npm module via the command line:

npm command line: Publish

Here's an example of uploading source maps using the npm module via the command line. Note that the source map can come from a local file or a remote URL.

npm install -g @newrelic/publish-sourcemap
publish-sourcemap [/User/coder/project/dist/overview_page.min.js or https://bam-ui.nr-assets.net/overview-64.min.js.map] https://bam-ui.nr-assets.net/overview-64.min.js --nrAdminKey=[NEW_RELIC_ADMIN_KEY] --applicationId=NEW_RELIC_APP_ID]
npm command line: List published maps

Here's an example of listing published source maps:

list-sourcemaps --applicationId=[ID] --nrAdminKey=[apiKey]

Options:
  --applicationId  Browser application id [APPLICATION_ID)
  --nrAdminKey     New Relic admin api key [NR_ADMIN_KEY]
npm command line: Delete

Here's an example of deleting a source map:

delete-sourcemap --applicationId=[APPLICATION_ID] --nrAdminKey=[API_KEY] --sourcemapId=[SOURCE_MAP_ID]

Options:
  --applicationId  Browser application id (APPLICATION_ID)
  --nrAdminKey     New Relic admin api key (NR_ADMIN_KEY)
  --sourcemapId    Unique id generated for a source map

Here are some examples of using the npm module to publish from client-side JavaScript:

npm via Node.js script: Publish

Here's an example of publishing a source map via a Node.js script:

var publishSourcemap = require(‘@newrelic/publish-sourcemap’).publishSourcemap 

publishSourcemap({
  sourcemapPath: ‘SOURCE_MAP_FULL_PATH’,
  javascriptUrl: 'JS_URL'
  applicationId: APPLICATION_ID,
  nrAdminKey: 'ADMIN_KEY'
}, function (err) { console.log(err || 'Sourcemap upload done')})
npm via Node.js script: List published maps

Here's an example of listing all published source maps:

var listSourcemaps = require(‘@newrelic/publish-sourcemap’).listSourcemaps

listSourcemaps({
  sourcemapPath: ‘SOURCE_MAP_FULL_PATH’,
  javascriptUrl: 'JS_URL',
  applicationId: APP_ID,
  nrAdminKey: 'API_KEY',
}, function (err, res) { console.log(err || res.body)})
npm via Node.js script: Delete

Here's an example of deleting a source map file via a Node.js script:

var deleteSourcemap = require(‘@newrelic/publish-sourcemap’).deleteSourcemap

deleteSourcemap({
  sourcemapId: 'SOURCE_MAP_ID',
  applicationId: APP_ID,
  nrAdminKey: 'API_KEY',
}, function (err) { console.log(err || 'Deleted source map')})

When you're done, go to the JS errors page in New Relic Browser, select an error grouping, and see if your error stack traces have been un-minified.

Use API via cURL

Below are some examples of using cURL to publish, list, and delete source maps:

cURL: Upload maps

An example of using API via cURL to publish maps to Browser:

curl -H "Newrelic-Api-Key: API_KEY" \ 
   -F "sourcemap=@SOURCE_MAP_PATH" \ 
   -F "javascriptUrl=JS_URL" \ 
   -F "releaseId=YOUR_RELEASE_ID" \ 
   -F "releaseName=YOUR_UI_PAGE" \

https://sourcemaps.service.newrelic.com/v2/applications/APP_ID/sourcemaps
cURL: List existing maps

Below is an example of how to get a list of source maps previously uploaded to New Relic via cURL. New Relic returns the source map's unique API SOURCEMAP_ID and its components:

curl \ 
-H "Newrelic-Api-Key: APIKEY" \ 
https://sourcemaps.service.newrelic.com/v2/applications/APP_ID/sourcemaps
cURL: Delete map

To delete a source map:

  • Use the GET endpoint to list existing source maps and locate the SOURCEMAP_ID.
  • Run the following command via cURL.
curl -X DELETE \
     -H "Newrelic-Api-Key: API_KEY" \
https://sourcemaps.service.newrelic.com/v2/applications/APP_ID/sourcemaps/SOURCEMAP_ID

When you're done, go to the JS errors page in New Relic Browser, select an error grouping, and see if your error stack traces have been un-minified.

Troubleshoot source maps

If you are having trouble generating source maps from your build system, or your errors in New Relic Browser are remaining minified, see the Troubleshooting section.

For more help

Recommendations for learning more: