---
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/getting-started-tutorial/initialize-sentry-sdk-backend/
---

# Add the Sentry SDK to Your Backend Project

**Step 3 of 5**

This section walks you through how to import the sample app into your local dev environment, then add and initialize the Sentry SDK. 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/getting-started-tutorial/generate-first-error.md).

## [1. Clone the Sample App](https://docs.sentry.io/product/sentry-basics/getting-started-tutorial/initialize-sentry-sdk-backend.md#1-clone-the-sample-app)

The sample app is a backend API built with Express and Node, using ECMAScript Modules (ESM).

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:

   If you have SSH set up:

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

   Otherwise:

   ```bash
   git clone https://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/getting-started-tutorial/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 app'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 Node SDK.

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

   *Other available variations of the above snippet: yarn, pnpm*

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:

   ```javascript
   import * as Sentry from "@sentry/node";

   Sentry.init({
     dsn: "<your_DSN_key>",
     // Capture 100% of transactions for tracing.
     tracesSampleRate: 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 it.

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

   Replace `<your_DSN_key>` in the code above with the DSN key value you copied from the backend project you created in the [previous section](https://docs.sentry.io/product/sentry-basics/getting-started-tutorial/create-new-project.md). This DSN should be **different** from the one you used on your frontend project — each project has its own unique DSN.

4. Save the file.

   The options set in `Sentry.init()` are called the SDK's configuration. The only required option is the DSN; the SDK supports many others, which you can read about in our [Configuration](https://docs.sentry.io/platforms/javascript/guides/node/configuration.md) docs. The configuration above enables Sentry's error monitoring and [**Tracing**](https://docs.sentry.io/product/tracing.md) features.

5. Open your `server.js` file, import `Sentry`, and set up the error handler after all controllers and before any other error middleware. You don't import `instrument.js` here — you'll load it with Node's `--import` flag when you start the app (in the next step):

```javascript
+    import * as Sentry from "@sentry/node";
   import express from "express";
   import cors from "cors";
   import productsRoute from "./routes/products.js";

   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!");
   });

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

+    // 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");
+    });

   const port = 3001;

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

## [3. Build and Run the Sample Application](https://docs.sentry.io/product/sentry-basics/getting-started-tutorial/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
   ```

   *Other available variations of the above snippet: yarn, pnpm*

2. Start the application:

   ```bash
   npm start
   ```

   *Other available variations of the above snippet: yarn, pnpm*

   The sample app's `start` script runs `node --import ./instrument.js server.js`. The `--import` flag runs `instrument.js` (your `Sentry.init()` call) *before* the rest of the app loads — which is required for Sentry to instrument an ESM app correctly. If you adapt this to your own ESM project, start it the same way.

   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.

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. You should see the "Hello, Express.js Server here!" message.

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

Well done! You now have a sample Express backend app running with the Sentry SDK initialized. Next, [Capture Your First Distributed Tracing Error](https://docs.sentry.io/product/sentry-basics/getting-started-tutorial/generate-first-error.md) to start using Sentry's distributed tracing.
