|
| 1 | +--- |
| 2 | +title: Add the Sentry SDK to Your Backend Project |
| 3 | +sidebar_order: 2 |
| 4 | +description: "Learn how to add the Sentry SDK to your backend codebase." |
| 5 | +--- |
| 6 | + |
| 7 | +This section walks you through how to import the sample application into your local development environment, add the Sentry SDK, and initialize it. |
| 8 | + |
| 9 | +If you're using your own source code, you can skip this section. Instead: |
| 10 | + |
| 11 | +- Select your [platform](/platforms/) and follow its **Getting Started** guide to add the Sentry SDK to your code. |
| 12 | +- Then, skip to the [next step](/guides/integrate-frontend/generate-first-error/). |
| 13 | + |
| 14 | +## 1. Clone the Sample Application |
| 15 | + |
| 16 | +The sample application is a basic backend application using Express and Node. |
| 17 | + |
| 18 | +1. Fork the [sample application's repository](https://github.com/getsentry/frontend-tutorial) on GitHub. |
| 19 | +{/* TODO: ADD CUSTOM REPO HERE */} |
| 20 | + |
| 21 | +1. Clone the forked repository to your local environment: |
| 22 | + |
| 23 | + ```bash |
| 24 | + git clone [email protected]: <your_username >/frontend-tutorial.git |
| 25 | + ``` |
| 26 | + |
| 27 | +1. Open the `backend-tutorial` project in your preferred code editor. |
| 28 | + |
| 29 | +## 2. Add the Sentry React SDK |
| 30 | + |
| 31 | +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 React SDK](https://github.com/getsentry/sentry-javascript/tree/develop/packages/react). |
| 32 | + |
| 33 | +1. Install the Sentry React SDK using NPM. |
| 34 | + |
| 35 | + Make sure you're in the `backend-tutorial` project folder. |
| 36 | + |
| 37 | + ```bash {tabTitle:npm} |
| 38 | + npm install --save @sentry/node @sentry/profiling-node |
| 39 | + ``` |
| 40 | + |
| 41 | + ```bash {tabTitle:yarn} |
| 42 | + yarn add @sentry/node @sentry/profiling-node |
| 43 | + ``` |
| 44 | + |
| 45 | + ```bash {tabTitle:pnpm} |
| 46 | + pnpm add @sentry/node @sentry/profiling-node |
| 47 | + ``` |
| 48 | + |
| 49 | +1. Import and initialize the SDK. |
| 50 | + |
| 51 | + Create a file at the root level of your project and call it `instrument.js` |
| 52 | + |
| 53 | + ```javascript {filename:instrument.js} |
| 54 | + const Sentry = require("@sentry/node"); |
| 55 | + const { nodeProfilingIntegration } = require("@sentry/profiling-node"); |
| 56 | + |
| 57 | + Sentry.init({ |
| 58 | + dsn: "sample_DSN_key", |
| 59 | + integrations: [ |
| 60 | + nodeProfilingIntegration(), |
| 61 | + ], |
| 62 | + // Tracing |
| 63 | + tracesSampleRate: 1.0, |
| 64 | + // Capture 100% of the transactions |
| 65 | + |
| 66 | + // Set sampling rate for profiling - this is relative to tracesSampleRate |
| 67 | + profilesSampleRate: 1.0, |
| 68 | + }); |
| 69 | + ``` |
| 70 | + |
| 71 | + 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. |
| 72 | + |
| 73 | +1. Add your DSN key to the Sentry SDK configuration. |
| 74 | + |
| 75 | + Paste in the DSN key value you copied from the project created in the [previous section](/product/sentry-basics/integrate-frontend/create-new-project/). |
| 76 | + {/* TODO: Update link here */} |
| 77 | + |
| 78 | + ```javascript {filename:instrument.js} |
| 79 | + Sentry.init({ |
| 80 | + dsn: "<your_DSN_key>", |
| 81 | + // ... |
| 82 | + }); |
| 83 | + ``` |
| 84 | + |
| 85 | +1. Save the file. |
| 86 | + |
| 87 | +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](/platforms/javascript/guides/react/configuration/) docs. |
| 88 | + |
| 89 | +The configuration above enables Sentry's error monitoring feature, as well as its [Performance](/platforms/javascript/guides/react/performance) (tracing) and [Session Replay](/platforms/javascript/guides/react/session-replay) features. |
| 90 | + |
| 91 | +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: |
| 92 | + |
| 93 | + ``` javascript {filename:instrument.js} |
| 94 | + // IMPORTANT: Make sure to import `instrument.js` at the top of your file. |
| 95 | + // If you're using ECMAScript Modules (ESM) syntax, use `import "./instrument.js";` |
| 96 | + require("./instrument.js"); |
| 97 | + |
| 98 | + // All other imports below |
| 99 | + // Import with `import * as Sentry from "@sentry/node"` if you are using ESM |
| 100 | + const Sentry = require("@sentry/node"); |
| 101 | + const express = require("express"); |
| 102 | + const productsRoute = require('./routes/products'); |
| 103 | + const cors = require('cors') |
| 104 | + |
| 105 | + const app = express(); |
| 106 | + |
| 107 | + app.use(express.static('public')); |
| 108 | + app.use(cors()); |
| 109 | + |
| 110 | + app.get('/', (req, res) => { |
| 111 | + res.send('<h1>Hello, Express.js Server here!</h1>'); |
| 112 | + }); |
| 113 | + |
| 114 | + // The error handler must be registered before any other error middleware and after all controllers |
| 115 | + Sentry.setupExpressErrorHandler(app); |
| 116 | + |
| 117 | + // Optional fallthrough error handler |
| 118 | + app.use(function onError(err, req, res, next) { |
| 119 | + // The error id is attached to `res.sentry` to be returned |
| 120 | + // and optionally displayed to the user for support. |
| 121 | + console.log('500?'); |
| 122 | + res.statusCode = 500; |
| 123 | + res.end(res.sentry + "\n"); |
| 124 | + }); |
| 125 | + |
| 126 | + app.use('/products', productsRoute); |
| 127 | + |
| 128 | + const port = 3001; |
| 129 | + |
| 130 | + app.listen(port, () => { |
| 131 | + console.log(`Server is running on port`); |
| 132 | + }); |
| 133 | + |
| 134 | + ``` |
| 135 | + |
| 136 | +## 3. Build and Run the Sample Application |
| 137 | + |
| 138 | +In the `frontend-tutorial` project folder: |
| 139 | + |
| 140 | +1. Install project dependencies. |
| 141 | + |
| 142 | + ```bash |
| 143 | + npm install |
| 144 | + ``` |
| 145 | + |
| 146 | +1. Start the application in develop mode. |
| 147 | + |
| 148 | + ```bash |
| 149 | + npm start |
| 150 | + ``` |
| 151 | + |
| 152 | + Once the application starts, you'll see a confirmation message similar to this one in your terminal: |
| 153 | + |
| 154 | + ```bash |
| 155 | + <i> [webpack-dev-server] Project is running at: |
| 156 | + <i> [webpack-dev-server] Loopback: http://localhost:3000/ |
| 157 | + ... |
| 158 | + webpack 5.87.0 compiled successfully in 1306 ms |
| 159 | + ``` |
| 160 | + |
| 161 | + > **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`. |
| 162 | +
|
| 163 | +1. Open the sample application in your browser. |
| 164 | + |
| 165 | + The sample app should be running at [http://localhost:3000/](http://localhost:3000/) or the URL output in your terminal in the last step. |
| 166 | + |
| 167 | +  |
| 168 | + |
| 169 | +## Next |
| 170 | + |
| 171 | +At this point, you have a sample React app running with the Sentry SDK initialized. Next, [Add the Sentry SDK to Your Backend Project](/product/sentry-basics/integrate-frontend/generate-first-error/) to start using Sentry's error monitoring feature. |
0 commit comments