Skip to content

Commit

Permalink
add axiom example
Browse files Browse the repository at this point in the history
  • Loading branch information
danphilibin committed Sep 18, 2024
1 parent ec329f2 commit 33b206f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/axiom-webhook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Axiom Webhook

This example creates a webhook handler for [Axiom](https://axiom.co) that sends a new chat message when a [monitor](https://axiom.co/docs/monitor-data/monitors#custom-webhook) is triggered or resolved.

_This code is provided as an example and is not intended to be deployed as-is. Adapt the code in `src/index.ts` to fit your use case and deployment environment._

### Prerequisites

A `CAMPSITE_API_KEY` environment variable is required.

Additionally, [follow these steps](https://axiom.co/docs/monitor-data/monitors#custom-webhook) to create a custom webhook notifier in Axiom.
Binary file added examples/axiom-webhook/axiom-integration-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions examples/axiom-webhook/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "axiom-webhook",
"version": "1.0.0",
"scripts": {
"clean": "rm -rf .turbo node_modules",
"format": "prettier --write \"**/*.{ts,md,yml,json}\""
},
"dependencies": {
"dotenv": "^16.4.5"
},
"devDependencies": {
"@types/node": "^20.16.5",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"typescript": "^5.5.4"
}
}
37 changes: 37 additions & 0 deletions examples/axiom-webhook/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const CAMPSITE_API_KEY = process.env.CAMPSITE_API_KEY
const CAMPSITE_ALERTS_THREAD_ID = 'sdcup465jb7y'

export default async function server(request: Request): Promise<Response> {
if (request.method !== 'POST') {
return new Response('Method not allowed', { status: 405 })
}

const payload = await request.json()

// Modify this to suit your needs, e.g. for threshold alerts vs. match alerts
const status = payload.action === 'Open' ? '🔴 Monitor triggered' : '🟢 Monitor resolved'

const message = `**${status}: ${payload.event.title}**\n${payload.event.body}`

try {
const campsiteResponse = await fetch(`https://api.campsite.com/v2/threads/${CAMPSITE_ALERTS_THREAD_ID}/messages`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${CAMPSITE_API_KEY}`
},
body: JSON.stringify({
content_markdown: message
})
})

if (!campsiteResponse.ok) {
throw new Error(`Campsite API error: ${campsiteResponse.statusText}`)
}

return new Response('OK', { status: 200 })
} catch (error) {
console.error('Error posting to Campsite:', error)
return new Response('Error posting to Campsite', { status: 500 })
}
}
3 changes: 3 additions & 0 deletions examples/axiom-webhook/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.json"
}

0 comments on commit 33b206f

Please sign in to comment.