# Get a push notification key for your server

> Have an Agent check your Apple team's APNs keys, make one if you need it, and hand your push server the Key ID, Team ID and key file.

Source: https://air.build/docs/build/get-a-push-notification-key/

Push notifications reach the people who use your app from a server: your own, or a push service you sign up for. To send them through Apple, that server needs an APNs key from your Apple team. The `asc` help says a push provider needs three values: the Key ID, the Team ID and the key's `.p8` file. An Agent can make the key and get you all three.

> [!NOTE]
> Apple's [Create a private key to access a service](https://developer.apple.com/help/account/keys/create-a-private-key/) page says making a key needs the Account Holder or Admin role in your Apple Developer team.

> [!NOTE]
> AirBuild has no screen for APNs keys. You ask an Agent in the Project's chat, and it uses `asc web push-keys` (see APNs push keys in [asc: web session commands](https://air.build/docs/cli/asc-web/#apns-push-keys)). These commands go through the Team owner's Apple sign-in held in AirBuild; see [asc: web session commands](https://air.build/docs/cli/asc-web/#the-web-session).
>
> None of AirBuild's services for your app sends push notifications for you: [workers](https://air.build/docs/cli/workers/), the command Agents use for AirHost and AirBuild's other services, has no push command.

## Before you start

- The Team's owner has connected an Apple Developer account and switched a team on. See [Connect your Apple Developer account](https://air.build/docs/app-store/connect-apple-developer-account/).
- Your app has push notifications turned on. That's a capability, and making a key doesn't turn it on. Ask the Agents to add push notifications to the app: see [Turn on iCloud, App Groups or another capability](https://air.build/docs/build/turn-on-an-app-capability/).
- You know which server or push service will send the notifications. That's where the key goes.

## What one key covers

- The `asc` help says one APNs key sends pushes to every app on your Apple team, in both sandbox and production. You don't need a key per app.
- Apple limits how many APNs keys a team can hold: see Apple's [Establishing a token-based connection to APNs](https://developer.apple.com/documentation/usernotifications/establishing-a-token-based-connection-to-apns).
- Apple's [Create a private key to access a service](https://developer.apple.com/help/account/keys/create-a-private-key/) page says a key's `.p8` file can be downloaded only once.

So check what your team has before you make a key.

## Check for a key you have

Ask in the Project's chat:

```text
Which APNs push keys does my Apple team have?
```

The Agent lists them with `asc web push-keys list`: each key's Key ID, its name, and `Can Download`, whether its file can still be downloaded. Listing downloads nothing and changes nothing.

| The list shows | What to do |
| --- | --- |
| No keys | [Make a key](https://air.build/docs/build/get-a-push-notification-key/#make-a-key). |
| A key whose file you or your server already have | Use that key. Give the server the same three values. |
| A key that can still be downloaded | Nobody has its file yet. [Download its file](https://air.build/docs/build/get-a-push-notification-key/#download-a-keys-file). |
| A key that can't be downloaded, and nobody has its file | The file is gone. Make a new key. If your team has two already, revoke one first in your Apple Developer account: see Apple's [Revoke, edit, and download keys](https://developer.apple.com/help/account/keys/revoke-edit-and-download-keys/). |

## Make a key

Ask, for example:

```text
Make an APNs push notification key called "Notes Push" and tell me its Key ID and our Team ID.
```

1. The Agent makes the key with `asc web push-keys create` and the name you gave. The command makes the key and uses up its one download at the same time. It runs only with `--confirm`, so the Agent asks you first.
2. It saves the `.p8` file in a folder on the computer it works on. The help says the file is never saved over another file, and its contents are never printed.
3. It tells you the Key ID and where it saved the file.

The Team ID is your Apple team's ID. **Settings** › **Developer** shows it beside each team; see [Developer](https://air.build/docs/settings/developer/). The Agent can also read it with `asc auth list`.

If your Apple Account belongs to more than one team, say which one the key is for. The Agent may ask with a card: see [Connect your Apple Developer account](https://air.build/docs/app-store/connect-apple-developer-account/#when-an-agent-asks-for-a-team).

## Download a key's file

For a key that can still be downloaded, ask:

```text
Download the file for the APNs key called "Notes Push".
```

The Agent saves it with `asc web push-keys download` and the key's ID. Apple hands out the file only once, so the command runs only with `--confirm`, and the Agent asks you first.

## Keep the file safe

The three values together let a server send notifications to every app on your Apple team. Treat the file like a password.

- The help says to keep the file out of the repository and out of logs. Ask the Agent to save it in a folder the project's `.gitignore` leaves out, such as `secrets/`, and to tell you its path. That keeps it out of the repository, a shared link and an export of the source: see [Share or export a project's source](https://air.build/docs/projects/share-and-export-source/#what-the-shared-source-contains). It still shows in **Files**, dimmed, where anyone in the Team can open it.
- To take a copy for your server or push service, open it in **Files** and use **Export** (see [Find the files and documents Agents made](https://air.build/docs/projects/find-files/#save-a-file-or-the-whole-project)), or ask the Agent to send it to you in your DM with that Agent (see [Create group chats and talk to one Agent](https://air.build/docs/projects/groups-and-dms/#talk-to-one-agent)), then touch and hold the file (on the Mac, right-click it) and choose **Save As…** (see [Send photos, files and sketches to an Agent](https://air.build/docs/projects/send-photos-and-files/#open-files-in-the-chat)). Don't ask for the file in the Project's chat: everyone in the chat can open it, and you can't recall a message the Agent sent.
- If the file is lost, it can't be downloaded again. Revoke the key in your Apple Developer account and make a new one. Agents can't revoke it: `asc web push-keys` has no command for that.

## Give it to your server

Your server or push service asks for the Key ID, the Team ID and the `.p8` file. Where you enter them depends on the service. For how a server of your own uses them to talk to Apple, see Apple's [Establishing a token-based connection to APNs](https://developer.apple.com/documentation/usernotifications/establishing-a-token-based-connection-to-apns).

A key the Agent makes works in both sandbox and production, so you don't need another one for testing.

## Questions

**Can I make a key in AirBuild without an Agent?**

No. AirBuild has no screen for APNs keys. Ask an Agent, or make one in your Apple Developer account on the web: see Apple's [Create a private key to access a service](https://developer.apple.com/help/account/keys/create-a-private-key/).

**The Agent says Apple refused the key because the team holds at most two.**

No key was made. The message goes on: "one existing key works for every app of the team, so reuse it if your user has its .p8, or ask them which key to revoke". Use one of your two keys, or revoke one in your Apple Developer account and ask again.

**The Agent says a key already has this name.**

The message is "already has this name; inspect it instead of creating a duplicate". Use that key, or give the new one another name.

**The Agent says the key exists, but the download failed.**

The key was made, but its file didn't arrive. Don't ask for a new key straight away: the help says a create is never retried on its own, and to check the list first. Ask the Agent to list the keys. If the key can still be downloaded, ask it to download the file; if not, revoke the key and make another.

**It says “Apple denied the request (403)” or that Apple refused the web session.**

The 403 message adds: "the account role likely lacks permission for this action, or the web session expired". If the Team owner's Apple sign-in has expired, the owner signs in again in **Settings** › **Developer** › **Sign In Again**: see [Connect your Apple Developer account](https://air.build/docs/app-store/connect-apple-developer-account/#sign-in-again). Then ask the Agent to try again.

**Can a Worker on AirHost send the notifications?**

AirHost has no push notification feature of its own, and the `workers` help doesn't say whether a Worker's code can reach Apple's push service. Ask the Agent in the chat before you plan on it.
