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.
Fork the sample application's repository on GitHub.
Clone the forked repository to your local environment:
Copiedgit clone git@github.com:<your_username>/frontend-tutorial.git
Open the
tracing-tutorial-backend
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 Node SDK.
Install the Sentry Express SDK using NPM.
Make sure you're in the
tracing-tutorial-backend
project folder.Copiednpm install --save @sentry/node @sentry/profiling-node
Import and initialize the SDK.
Create a file at the root level of your project and call it
instrument.js
instrument.js
Copiedconst 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.
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. 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
CopiedSentry.init({ dsn: "<your_DSN_key>", // ... });
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 [Tracing](will add link later) and Session Replay features.
Be sure to 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
// 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`);
});
In the tracing-tutorial-backend
project folder:
Install project dependencies.
Copiednpm install
Start the application in develop mode.
Copiednode server.js
Once the application starts, you'll see a confirmation message similar to this one in your terminal:
CopiedServer 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. Run
nvm use 18
and thennpm install
.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.
Well done! You now 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.
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").