Add the Sentry SDK to Your Frontend Project
Learn how to add the Sentry SDK to your frontend 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 frontend-only application using React and webpack.
Fork the sample application's repository on GitHub.
Clone the forked repository to your local environment:
Copiedgit clone git@github.com:<your_username>/tracing-tutorial-frontend.git
Open the
tracing-tutorial-frontend
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 React SDK.
Install the Sentry React SDK using NPM.
Make sure you're in the
tracing-tutorial-frontend
project folder.Copiednpm install @sentry/react --save
Import and initialize the SDK.
Open
src/index.js
and add the following lines of code below the last import statement:src/index.js
Copiedimport * as Sentry from "@sentry/react"; Sentry.init({ dsn: "your_DSN_key", integrations: [ Sentry.browserTracingIntegration(), Sentry.replayIntegration(), ], // Tracing tracesSampleRate: 1.0, // Capture 100% of the transactions // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled tracePropagationTargets: ["localhost", /^https:\/\/yourserver\.io\/api/], // Session Replay replaysSessionSampleRate: 0.1, // This sets the sample rate at 10%. You may want to change it to 100% while in development and then sample at a lower rate in production. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur. });
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.
src/index.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 tracing and Session Replay features. Take note of the tracePropagationTargets
option, this is required to enable tracing on any urls your projects are running on. Since both of this tutorial's projects are running on localhost
, we are all set.
In the tracing-tutorial-frontend
project folder:
Install project dependencies.
Copiednpm install
Start the application in develop mode.
Copiednpm start
Once the application starts, you'll see a confirmation message similar to this one in your terminal:
CopiedCompiled successfully! You can now view tracing-demo in the browser. Local: http://localhost:3000 On Your Network: http://10.0.0.39:3000
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, ensure you are on the right node version with
nvm use 18
and thennpm install
.Open the sample application in your browser.
The sample app should be running at http://localhost:3000/ or the URL output in your terminal in the last step. You should see a sample e-commerce page; the buttons on this page won't work correctly and will trigger an uncaught runtime error until you get your backend up and running.
Nicely done! You now have a sample React app running with the Sentry SDK initialized. Next, Add the Sentry SDK to Your Backend Project to get Sentry running across the entire stack of your platform.
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").