Add the Sentry SDK to Your Backend Project

Learn how to add the Sentry SDK to your backend codebase.

This section walks you through how to import the sample application into your local development environment, add the Sentry SDK, and initialize it.

If you're using your own source code, you can skip this section. Instead:

  • Select your platform and follow its Getting Started guide to add the Sentry SDK to your code.
  • Then, skip to the next step.

The sample application is a basic backend application using Express and Node.

  1. Fork the sample application's repository on GitHub.
  1. Clone the forked repository to your local environment:

    Copied
    git clone git@github.com:<your_username>/frontend-tutorial.git
    
  2. Open the backend-tutorial project in your preferred code editor.

Sentry captures data by using a platform-specific SDK that you add to your application's runtime. To use the SDK, import and configure it in your source code. This demo project uses Sentry's Express SDK.

  1. Install the Sentry Express SDK using NPM.

    Make sure you're in the backend-tutorial project folder.

    Copied
    npm install --save @sentry/node @sentry/profiling-node
    
  2. Import and initialize the SDK.

    Create a file at the root level of your project and call it instrument.js

    instrument.js
    Copied
    const Sentry = require("@sentry/node");
    const { nodeProfilingIntegration } = require("@sentry/profiling-node");
    
    Sentry.init({
      dsn: "sample_DSN_key",
      integrations: [nodeProfilingIntegration()],
      // Tracing
      tracesSampleRate: 1.0,
      //  Capture 100% of the transactions
    
      // Set sampling rate for profiling - this is relative to tracesSampleRate
      profilesSampleRate: 1.0,
    });
    

    It's important to import and initialize the SDK as early as possible in your application's lifecycle so Sentry can capture errors throughout the lifecycle.

  3. Add your DSN key to the Sentry SDK configuration.

    Paste in the DSN key value you copied from the project created in the previous section.

    instrument.js
    Copied
    Sentry.init({
      dsn: "<your_DSN_key>",
      // ...
    });
    
  4. Save the file.

The options set in Sentry.init() are called the SDK's configuration. The only required configuration option is the DSN. However, the SDK supports many other configuration options. Learn more in our Configuration docs.

The configuration above enables Sentry's error monitoring feature, as well as its Performance (tracing) and Session Replay features.

Make sure to import instrument.js at the top of your server.js file, and set up the error handler after all controllers and before any other middleware:

server.js
Copied
// IMPORTANT: Make sure to import `instrument.js` at the top of your file.
// If you're using ECMAScript Modules (ESM) syntax, use `import "./instrument.js";`
require("./instrument.js");

// All other imports below
// Import with `import * as Sentry from "@sentry/node"` if you are using ESM
const Sentry = require("@sentry/node");
const express = require("express");
const productsRoute = require("./routes/products");
const cors = require("cors");

const app = express();

app.use(express.static("public"));
app.use(cors());

app.get("/", (req, res) => {
  res.send("<h1>Hello, Express.js Server here!</h1>");
});

// The error handler must be registered before any other error middleware and after all controllers
Sentry.setupExpressErrorHandler(app);

// Optional fallthrough error handler
app.use(function onError(err, req, res, next) {
  // The error id is attached to `res.sentry` to be returned
  // and optionally displayed to the user for support.
  console.log("500?");
  res.statusCode = 500;
  res.end(res.sentry + "\n");
});

app.use("/products", productsRoute);

const port = 3001;

app.listen(port, () => {
  console.log(`Server is running on port 3001`);
});

In the frontend-tutorial project folder:

  1. Install project dependencies.

    Copied
    npm install
    
  2. Start the application in develop mode.

    Copied
    npm start
    

    Once the application starts, you'll see a confirmation message similar to this one in your terminal:

    Copied
    <i> [webpack-dev-server] Project is running at:
    <i> [webpack-dev-server] Loopback: http://localhost:3001/
    ...
    webpack 5.87.0 compiled successfully in 1306 ms
    

    Troubleshooting tip: If the application fails to start due to syntax errors or errors for missing dependencies/modules, make sure you're using Node 18+ and install dependencies again. Run nvm use 18 and then npm install.

  3. Open the sample application in your browser.

    The sample app should be running at http://localhost:3001/ or the URL output in your terminal in the last step.

At this point, you have a sample Express backend app running with the Sentry SDK initialized. Next, Generate your first cross-project error to start using Sentry's Distributed Tracing feature.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").