---
title: "Add the Sentry SDK to Your Backend Project"
description: "Learn how to add the Sentry SDK to your backend codebase."
url: https://docs.sentry.io/product/sentry-basics/distributed-tracing/initialize-sentry-sdk-backend/
---

# Add the Sentry SDK to Your Backend Project

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](https://docs.sentry.io/platforms.md) and follow its **Getting Started** guide to add the Sentry SDK to your code.
* Then, skip to the [next step](https://docs.sentry.io/product/sentry-basics/distributed-tracing/generate-first-error.md).

## [1. Clone the Sample Application](https://docs.sentry.io/product/sentry-basics/distributed-tracing/initialize-sentry-sdk-backend.md#1-clone-the-sample-application)

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

1. Fork the [sample application's repository](https://github.com/getsentry/tracing-tutorial-backend) on GitHub.

2. Clone the forked repository to your local environment:

   ```bash
   git clone git@github.com:<your_username>/tracing-tutorial-backend.git
   ```

3. Open the `tracing-tutorial-backend` project in your preferred code editor.

## [2. Add the Sentry Node SDK](https://docs.sentry.io/product/sentry-basics/distributed-tracing/initialize-sentry-sdk-backend.md#2-add-the-sentry-node-sdk)

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 Node SDK](https://github.com/getsentry/sentry-javascript/tree/master/packages/node).

1. Navigate to the `tracing-tutorial-backend` project folder and install the Sentry Express SDK using NPM.

   ```bash
   npm install --save @sentry/node @sentry/profiling-node
   ```

2. Create a file at the root level of your project and call it `instrument.js`. Import and initialize the SDK using the following code:

   `instrument.js`

   ```javascript
   const Sentry = require("@sentry/node");
   const { nodeProfilingIntegration } = require("@sentry/profiling-node");

   Sentry.init({
     dsn: "sample_DSN_key",
     integrations: [nodeProfilingIntegration()],
     // Tracing
     // Capture 100% of the transactions
     tracesSampleRate: 1.0,

     // 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](https://docs.sentry.io/product/sentry-basics/distributed-tracing/create-new-project.md). NOTE: This DSN key should be **different** from the one you used to configure Sentry on your frontend project. Your frontend and backend projects should each have a unique DSN.

   `instrument.js`

   ```javascript
   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](https://docs.sentry.io/platforms/javascript/guides/react/configuration.md) docs.

   The configuration above enables Sentry's error monitoring feature, as well as its [**Tracing**](https://docs.sentry.io/product/tracing.md) and [**Session Replay**](https://docs.sentry.io/platforms/javascript/guides/react/session-replay.md) features.

5. Import `instrument.js` at the top of your `server.js` file. Then import `Sentry` and set up the error handler after all controllers and before any other middleware:

`server.js`

```javascript
+    // 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>');
   });

   app.get("/products/debug-sentry", (req, res) => {
     console.log("Sentry Error thrown!");
     throw new Error("My first Sentry error!");
   });

+    // 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 error thrown!');
+        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`);
   });
```

## [3. Build and Run the Sample Application](https://docs.sentry.io/product/sentry-basics/distributed-tracing/initialize-sentry-sdk-backend.md#3-build-and-run-the-sample-application)

In the `tracing-tutorial-backend` project folder:

1. Install project dependencies.

   ```bash
   npm install
   ```

2. Start the application in develop mode:

   ```bash
   node server.js
   ```

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

   ```bash
   Server is running on port 3001
   ```

> **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. If you're using [nvm](https://github.com/nvm-sh/nvm), ensure you are on the right node version with `nvm use 18` and then `npm install`.

1. 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.

## [Next](https://docs.sentry.io/product/sentry-basics/distributed-tracing/initialize-sentry-sdk-backend.md#next)

Well done! You now have a sample Express backend app running with the Sentry SDK initialized. Next, [Generate your first cross-project error](https://docs.sentry.io/product/sentry-basics/distributed-tracing/generate-first-error.md) to start using Sentry's Distributed Tracing feature.
