Back to APIs

Manage payments, invoices, and more with Square's APIs.

Using the Square API with Trigger.dev

You can use Trigger.dev with any existing Node SDK or even just using fetch. Using io.runTask makes your Square background job resumable and appear in our dashboard.

Use io.runTask() and the official SDK or fetch.

Use our HTTP endpoint to subscribe to webhooks

Example code using Square

Below is a working code example of how you can use Square with Trigger.dev. These samples are open source and maintained by the community, you can copy and paste them into your own projects.

1
import { TriggerClient } from "@trigger.dev/sdk";
2
import { WebhooksHelper } from "square";
3
import { Slack } from "@trigger.dev/slack";
4
5
const slack = new Slack({ id: "slack" });
6
7
// Go to your Square developer account
8
// Create and open an application
9
// Go to Webhooks > Subscriptions
10
// Add a subscription and add your trigger webhooks url in url field
11
// Select events you want to be notified about
12
// Save the webhook
13
// Obtain the Webhook Signature Key and Notification URL
14
15
// Create an HTTP Endpoint, with the Square details
16
const square = client.defineHttpEndpoint({
17
id: "square",
18
source: "square.com",
19
icon: "square",
20
verify: async (request) => {
21
const body = await request.text();
22
23
const isFromSquare = WebhooksHelper.isValidWebhookEventSignature(
24
body,
25
request.headers.get("x-square-hmacsha256-signature") ?? "",
26
process.env.SQUARE_WEBHOOK_SIGNATURE_KEY ?? "",
27
process.env.SQUARE_WEBHOOK_NOTIFICATION_URL ?? ""
28
);
29
30
if (!isFromSquare) {
31
return {
32
success: false,
33
reason: "Invalid Square Signature",
34
};
35
} else {
36
return {
37
success: true,
38
};
39
}
40
},
41
});
42
43
//Our job sends a Slack message when customer is created or deleted
44
client.defineJob({
45
id: "http-square",
46
name: "HTTP Square",
47
version: "1.0.0",
48
enabled: true,
49
// Create a trigger from the HTTP endpoint
50
trigger: square.onRequest(),
51
integrations: {
52
slack,
53
},
54
run: async (request, io, ctx) => {
55
const body = await request.json();
56
await io.logger.info(`Body`, body);
57
58
const customer = body.data.object.customer;
59
60
switch (body.type) {
61
case "customer.created": {
62
await io.slack.postMessage("customer-created", {
63
channel: process.env.SLACK_CHANNEL!,
64
text: `Customer created:\n ${customer.given_name} ${customer.family_name}`,
65
});
66
break;
67
}
68
case "customer.deleted": {
69
await io.slack.postMessage("customer-deleted", {
70
channel: process.env.SLACK_CHANNEL!,
71
text: `Customer deleted:\n ${customer.given_name} ${customer.family_name}`,
72
});
73
break;
74
}
75
}
76
},
77
});