# Buttons and actions

> Decide what happens when someone taps a push, and add your own buttons that open links or call your API.

A push can do more than show text. You can choose what a tap does, what the Copy button copies, and add up to two buttons of your own, such as **Approve** and **Reject**.

## What a tap does [#what-a-tap-does]

| You send | Tapping the notification |
|---|---|
| No `url` | Opens the push in the Opush app |
| `url` | Opens the link |
| `url` and `action=none` | Opens the Opush app only. The link is not followed. |

```bash
# Tap to open the failing build
curl "https://opu.sh/YOUR_KEY/Build%20failed/main%20%23128?url=https://ci.example.com/builds/128"
```

`action=alert` is also accepted.

## Built-in buttons [#built-in-buttons]

Long-press a notification to see its buttons. Opush adds these for you:

| Push | Buttons |
|---|---|
| Without `url` | **Copy** |
| With `url` | **Open link**, **Copy** |

**Copy** copies the `copy` value if you sent one, otherwise the body. This is handy for one-time codes:

```bash
curl "https://opu.sh/YOUR_KEY/Login%20code/Your%20code%20is%20381920?copy=381920"
```

`autoCopy` is accepted for existing scripts, but its behavior is not guaranteed. Use `copy` and the Copy button instead.

## Custom buttons [#custom-buttons]

Add up to two buttons with `button1` and `button2`. Each button needs a label and a URL.

| Parameter | Description |
|---|---|
| `button1`, `button2` | Label, up to 40 characters |
| `button1_url`, `button2_url` | Required. The link to open, or the request to send |
| `button1_method`, `button2_method` | Optional. `GET` or `POST` sends a request in the background instead of opening the link |
| `button1_style`, `button2_style` | Optional. `destructive` shows the button in red |

On the notification, your buttons come first, then **Open link** (if there's a `url`), then **Copy**.

### Buttons that open a link [#buttons-that-open-a-link]

Without a method, the button opens its URL. It can be a web page or another app's URL scheme.

```bash
curl https://opu.sh/YOUR_KEY \
  --data-urlencode "title=New pull request" \
  --data-urlencode "body=#42 Fix login redirect" \
  --data-urlencode "button1=Review" \
  --data-urlencode "button1_url=https://github.com/acme/app/pull/42"
```

### Buttons that call your API [#buttons-that-call-your-api]

With `GET` or `POST`, pressing the button sends a request from the phone in the background. Nothing opens. This is useful for approvals.

In JSON you can write the buttons as an array:

```bash
curl -X POST https://opu.sh/push \
  -H "Content-Type: application/json" \
  -d '{
    "device_key": "YOUR_KEY",
    "id": "ticket-42",
    "title": "Refund request #42",
    "body": "$49.00 for order 1042",
    "buttons": [
      { "title": "Approve", "url": "https://example.com/tickets/42/approve?token=SECRET", "method": "POST" },
      { "title": "Reject", "url": "https://example.com/tickets/42/reject?token=SECRET", "method": "POST", "style": "destructive" }
    ]
  }'
```

How it works:

- The device must be unlocked first, so nobody can press **Approve** from your Lock Screen.
- The request goes straight from the phone to your URL. It does not pass through Opush.
- When the request finishes, the app shows a short notification with the result (success, or failure with the status code).

A `POST` request has a JSON body with `Content-Type: application/json`:

```json
{ "button": "Approve", "index": 1, "id": "ticket-42", "idempotency_key": "opush-ticket-42" }
```

`id` is the push's `id` parameter, or `null` if you didn't send one. A `GET` request has no body.

<Callout type="warn" title="Protect the endpoint">
  Anyone who sees the URL could call it. Use `https` and put a secret token in the URL, as in the example above. If you're worried about the token passing through Opush, [encrypt the push](/docs/push/parameters#encryption). Buttons can go inside the encrypted part.
</Callout>

## Protection against double presses [#protection-against-double-presses]

Pressing **Approve** twice can cause real problems, so the app guards against it:

1. **While sending**: other buttons and repeated taps on the same push do nothing until the request returns.
2. **After success**: when your server returns a `2xx` status, or `409` (meaning "already handled"), the request buttons for that push are locked:
   - Notifications for that push are removed from Notification Center, including copies with the same `id`.
   - If a copy is still around and gets pressed, the app doesn't send the request. It tells you it was already sent.
   - In the app, the push shows which button was sent. To send again, tap **Send again** and confirm.
3. **Idempotency key**: every request has an `Idempotency-Key` header, and `POST` bodies also include `idempotency_key`. All buttons on one push share the same key. If the push has an `id`, the key is `opush-<id>`, the same on every device that received it.

Buttons that only open a link are never locked.

### Handle duplicates on your server [#handle-duplicates-on-your-server]

Use the idempotency key to make your endpoint safe to call twice. Process the first request. For any later request with the same key, return `409`. The app then shows that the request was already handled.

```js
// Example with a key-value store
app.post("/tickets/:id/approve", async (req, res) => {
  const key = req.get("Idempotency-Key");
  if (key && (await store.get(key))) return res.status(409).end();

  await approveTicket(req.params.id);
  if (key) await store.set(key, "done");
  res.status(200).end();
});
```

## Errors [#errors]

Opush checks buttons before sending. These return `400` and the push is not sent:

- More than two buttons
- A button without a label or without a URL
- A URL that isn't valid
- A method other than `GET` or `POST`
- A background request (`GET` or `POST`) to a URL that isn't `http` or `https`
