# Sentry SDK

You can collect errors by integrating your [Sentry SDK](https://docs.sentry.io/platforms/) with Better Stack. This allows you to **leverage your existing Sentry setup** to send errors to our platform for centralized management and analysis.

[info]
#### Using Claude Code or similar agentic dev-tool?

The easiest way to integate is to use the [Integrate with AI](https://betterstack.com/docs/errors/collecting-errors/ai-prompt/) prompt.
[/info]

To configure Sentry SDK integration:

1.  Navigate to **Errors** -> [Applications](https://errors.betterstack.com/team/t0/applications/ ";_blank").
2.  Select your application.
3.  Go to the **Data ingestion** tab.
4.  Copy **Application token** and **Ingesting host** to be used in Sentry SDK.

## Using the Sentry SDK

To send errors to Better Stack, configure your Sentry SDK according to the **Data ingestion** tab for your application:

```plain
[label Sentry DSN configuration]
https://$APPLICATION_TOKEN@$INGESTING_HOST/1
```

[info]
The project ID at the end of the DSN (`/1`) is required by Sentry SDKs but is ignored by our ingestion endpoint. You can use any number.
[/info]

Here are a few examples for popular Sentry SDKs:

[code-tabs]
```python
[label Python]
import sentry_sdk

sentry_sdk.init(
    dsn="https://$APPLICATION_TOKEN@$INGESTING_HOST/1",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for performance monitoring.
    traces_sample_rate=1.0,
)

# Example of capturing an exception
try:
    1 / 0
except ZeroDivisionError as e:
    sentry_sdk.capture_exception(e)
```
```javascript
[label Node.js]
const Sentry = require("@sentry/node");
// Or, for ES6+
// import * as Sentry from "@sentry/node";

Sentry.init({
  dsn: "https://$APPLICATION_TOKEN@$INGESTING_HOST/1",
  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for performance monitoring.
  tracesSampleRate: 1.0,
});

// Example of capturing an exception
try {
  throw new Error("This is a test error from Node.js!");
} catch (e) {
  Sentry.captureException(e);
}
```
```javascript
[label Browser (JS)]
import * as Sentry from "@sentry/browser";

Sentry.init({
  dsn: "https://$APPLICATION_TOKEN@$INGESTING_HOST/1",
  // To capture performance monitoring data,
  // you must set tracesSampleRate
  tracesSampleRate: 1.0,
});

// Example of capturing an exception
try {
  throw new Error("This is a test error from the browser!");
} catch (e) {
  Sentry.captureException(e);
}
```
```ruby
[label Ruby]
require 'sentry-ruby'

Sentry.init do |config|
  config.dsn = 'https://$APPLICATION_TOKEN@$INGESTING_HOST/1'
  config.breadcrumbs_logger = [:active_support_logger, :http_logger]

  # To activate performance monitoring, set one of these options.
  # We recommend adjusting the value in production:
  config.traces_sample_rate = 1.0
end

# Example of capturing an exception
begin
  1 / 0
rescue ZeroDivisionError => exception
  Sentry.capture_exception(exception)
end
```
```php
[label PHP]
use function Sentry\init;
use function Sentry\captureException;

// Assumes you have the Sentry SDK installed via Composer
require 'vendor/autoload.php';

init(['dsn' => 'https://$APPLICATION_TOKEN@$INGESTING_HOST/1' ]);

// Example of capturing an exception
try {
    throw new \Exception("This is a test error from PHP!");
} catch (\Exception $e) {
    captureException($e);
}
```
```go
[label Go]
package main

import (
	"errors"
	"log"
	"time"

	"github.com/getsentry/sentry-go"
)

func main() {
	err := sentry.Init(sentry.ClientOptions{
		Dsn: "https://$APPLICATION_TOKEN@$INGESTING_HOST/1",
		// Set TracesSampleRate to 1.0 to capture 100%
		// of transactions for performance monitoring.
		TracesSampleRate: 1.0,
	})
	if err != nil {
		log.Fatalf("sentry.Init: %s", err)
	}

	// Flush buffered events before the program terminates.
	defer sentry.Flush(2 * time.Second)

	// Example of capturing an exception
	sentry.CaptureException(errors.New("This is a test error from Go!"))
}
```
[/code-tabs]

## Using a different platform?
* Read the [official Sentry SDK documentation](https://docs.sentry.io).
* Make sure to **change the DSN** to send the data to Better Stack.

## Supported Sentry SDK versions

The Sentry SDK versions listed below are the minimum supported. If you're using an older version, please upgrade before sending data to Better Stack.

| Platform | Minimum Version |
|----------|---------|
| JavaScript | 7.0.0 |
| Python | 2.0.0 |
| Ruby | 4.0.0 |
| Java/Android | 3.0.0 |
| Cocoa (iOS/macOS) | 6.0.0 |
| .NET | 3.0.0 |
| PHP | 4.0.0 |
| Go | 0.1.0 |
| React Native | 3.0.0 |

## See the error in Better Stack

After running the script, you should see your first report in [Errors](https://errors.betterstack.com/0/errors ";_blank") 🚀

![First error in Better stack](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/5163c4ac-6b4e-4963-ddcc-41217c77e900/orig =4160x1376)
