Your first API app: a two-way SMS service in an afternoon
Receive a message on your short code, work out a reply and send it back. The whole loop, in about forty lines of Node.
Most API apps on AppLink start with the same loop: a subscriber sends a message, your server decides what it means, and a reply goes back. Get that loop working and everything else, from subscriptions to charging, is an addition to it rather than a new problem.
How the loop works
When a subscriber texts your keyword to the short code, AppLink sends the message to the receive URL you set while provisioning the app, as a mobile-originated (MO) request. Your server answers that request with a status code, then calls the send endpoint with a mobile-terminated (MT) reply. Two HTTP requests: one in, one out.
Before you write code
- Create a free developer account and complete your Service Provider profile.
- Provision a new app with the SMS API selected, and set its receive URL to a public HTTPS address on your server.
- Note your applicationId. The password (your API key) arrives by email once the app is approved.
The handler
Here is the whole service in Node with Express. It acknowledges every inbound message, builds a reply from the text, and sends it back to the number that wrote in, asking for a delivery report.
import express from "express";
const API = "https://api.applink.com.bd";
const { APP_ID, APP_PASSWORD } = process.env;
const app = express();
app.use(express.json());
// AppLink calls this URL with every message sent to your short code.
app.post("/sms/receive", async (req, res) => {
const { sourceAddress, message } = req.body;
// Acknowledge first, so AppLink is not kept waiting on your logic.
res.json({ statusCode: "S1000", statusDetail: "Success" });
const result = await fetch(`${API}/sms/send`, {
method: "POST",
headers: { "Content-Type": "application/json;charset=utf-8" },
body: JSON.stringify({
version: "1.0",
applicationId: APP_ID,
password: APP_PASSWORD,
message: buildReply(message),
destinationAddresses: [sourceAddress],
deliveryStatusRequest: "1",
}),
}).then((r) => r.json());
if (result.statusCode !== "S1000") {
console.error("send failed", result.statusCode, result.statusDetail);
}
});
function buildReply(text = "") {
if (/\bHELP\b/i.test(text)) return "Text RESULT and your roll number to get your result.";
return "Thanks, we got your message.";
}
app.listen(3000);What to watch for
- Answer the inbound request quickly, then do the slow work. A receive URL that hangs looks like an outage.
- Keep the password on the server, in an environment variable. Never ship it in a mobile app or a web page.
- Check statusCode on every response. S1000 is success; anything starting with E has a meaning in the status code table.
- Log the requestId with every call. It is the fastest way to trace a message when you ask for support.
Where to go next
Once replies flow, add the Subscription API so that a subscriber who texts a keyword joins a daily or monthly service, and listen for subscription notifications to know when someone joins or leaves. That is the step that turns a reply bot into recurring revenue.
Launch your own service on AppLink today.