Testing the Sentry API: read issues and check releases from CI

Sentry tells you that production is throwing errors. Its API lets a pipeline ask the same question automatically: did the release we just deployed bring new issues? That turns Sentry from a dashboard people check into a gate the deploy has to pass.

Step 1: Token and environment

In Sentry, Settings ▸ Developer Settings ▸ Organization Tokens (or a personal token with project:read, event:read and org:read).

VariableValue
sentryBasehttps://sentry.io/api/0
sentryTokensntrys_…, marked as secret
sentryOrgyour organization slug
sentryProjectorders-api

Authorization: Bearer token with {{sentryToken}}. If the organization is in the EU region, use the de.sentry.io host; the other one answers 404 for its projects.

Step 2: Projects

HTTP
GET {{sentryBase}}/organizations/{{sentryOrg}}/projects/
JavaScript
const slugs = pm.response.json().map((p) => p.slug);
pm.test('project visible to the token', () => pm.expect(slugs).to.include(pm.environment.get('sentryProject')));

Keep the trailing slash: Sentry's API paths end with /.

Step 3: Unresolved issues

HTTP
GET {{sentryBase}}/projects/{{sentryOrg}}/{{sentryProject}}/issues/?query=is:unresolved&statsPeriod=24h&limit=25
JavaScript
const issues = pm.response.json();
console.log(issues.map((i) => `${i.shortId} ${i.count}x ${i.title}`).join('\n'));
pm.test('no fatal issues in the last 24h', () => {
  pm.expect(issues.filter((i) => i.level === 'fatal')).to.have.lengthOf(0);
});

The query parameter takes the same search syntax as the Sentry UI, so you can build the query there and paste it.

Step 4: Pagination

Sentry paginates with a cursor in the Link header:

Output
<https://sentry.io/api/0/...&cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1",
<https://sentry.io/api/0/...&cursor=0:25:0>; rel="next"; results="true"; cursor="0:25:0"
JavaScript
const link = pm.response.headers.get('Link') || '';
const next = link.match(/rel="next"; results="true"; cursor="([^"]+)"/);
if (next) pm.environment.set('cursor', next[1]);

rel="next" is always present. results="true" is what tells you there's another page. Checking for rel="next" alone loops forever.

Step 5: A release gate

After a deploy tagged with release [email protected]:

HTTP
GET {{sentryBase}}/projects/{{sentryOrg}}/{{sentryProject}}/issues/?query=is:unresolved firstRelease:{{release}}&limit=100
JavaScript
const fresh = pm.response.json();
const threshold = Number(pm.environment.get('maxNewIssues') || 3);
pm.test(`at most ${threshold} new issues in ${pm.environment.get('release')}`, () => {
  pm.expect(fresh.length).to.be.at.most(threshold);
});

In the pipeline, wait a few minutes after the deploy so traffic arrives, then:

Terminal
requestarium run "New issues in release" --env Sentry --env-var sentryToken=SENTRY_TOKEN --var release=orders-api@$VERSION

A non-zero exit fails the job, and the rollback decision has data behind it. Pair it with smoke tests after deploy.

Errors

401 for a bad token, 403 when its scopes don't cover the call, 404 for a wrong organization or project slug (or the wrong region), and 429 when rate limited, with X-Sentry-Rate-Limit-* headers.