> For the complete documentation index, see [llms.txt](https://docs.thunkable.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.thunkable.com/~/changes/wHhkjLVeheBnfbpghRnw/in-app-purchases/host-your-server-side-verification-code-on-firebase/setup-your-cloud-environment/ios-code.md). Agents can also search and read this documentation through its [MCP server](https://docs.thunkable.com/~gitbook/mcp).

# iOS Code

Please be mindful. This page is almost very similar but different than the last page. Differences in service account, code, package.json.

These codes will allow you to acknowledge and verify purchases or subscriptions. There is 1 call for each type of transaction.&#x20;

### 1. To get started, go to[ console.google.cloud.com/functions](https://console.cloud.google.com/functions/list)

### 2. Click Create Function

<div align="left"><figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2Fre0BCXHvaIHQNuAAKU5c%2F1.png?alt=media&amp;token=81cf7604-b792-4ed3-9f4b-428820a8aca6" alt=""><figcaption></figcaption></figure></div>

### 3. Give your new function  a meaningful name

**verifyiOSPurchase** should be the name you use here

<div align="left"><figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FvST2lqzhQKG59oz9s1pA%2F3.png?alt=media&amp;token=abc851bf-c29d-4c5e-9916-88eccae36ece" alt=""><figcaption></figcaption></figure></div>

### 4. Save the trigger type

<div align="left"><img src="broken://files/ljA2REgqWBnjuXZCw1Ee" alt=""> <figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2Fkxs0qXc9PQcsLH7zqkce%2F4.png?alt=media&amp;token=1d096d13-9ac3-4a35-a7d0-3d89fa5569eb" alt=""><figcaption></figcaption></figure></div>

### 5. Copy the code below and click the blue "next" button

<details>

<summary>Copy Me</summary>

{% code title="androidPurchaseHandler." %}

```
  const axios = require("axios");
  const functions = require("firebase-functions");
  const admin = require("firebase-admin");
  admin.initializeApp();
  
exports.verifyiOSPurchase = functions.https.onRequest((req, res) => {


  const {transactionID, testing, password} = req.body;
  const url = testing ? "https://sandbox.itunes.apple.com/verifyReceipt" : "https://buy.itunes.apple.com/verifyReceipt";
  functions.logger.info(`Verifying iOS purchase with transactionID: ${transactionID}\nTesting is occuring: ${testing} for user ${userID}`);

  const data = JSON.stringify({
    "receipt-data": transactionID,
    "password": password,
    "exclude-old-transactions": true,
  });

  const config = {
    method: "post",
    url: url,
    headers: {
      "Content-Type": "application/json",
    },
    data: data,
  };

  axios(config)
      .then(function(response) {
        res.json(response.data).status(200);
      })
      .catch(function(error) {
        functions.logger.log(error);
        res.send(error).status(500);
      });
});
```

{% endcode %}

</details>

<div align="left"><figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FF5fYazR0snHKxCjrD9ez%2F5.png?alt=media&amp;token=59426954-d827-4745-a4b5-35cdaa9f5589" alt=""><figcaption></figcaption></figure></div>

### 6. Clear the default and paste the copied code into the code

{% embed url="<https://vimeo.com/698771346>" %}

### 7. Update the package.json file

<details>

<summary>copy and paste this over  the default code</summary>

```
{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {
     "axios": "^0.26.0",
     "firebase-functions": "^3.18.0",
     "firebase-admin": "^10.0.2"
   }
}

```

</details>

{% embed url="<https://player.vimeo.com/video/698770935>" %}

### 8. Set the name of the functions entry point

the name used here should match the name used in **Step 3** above

<div align="left"><figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FJRTlI9GLUF27Vsg4mmiC%2F8.png?alt=media&amp;token=5c752061-c20d-404b-8841-df773b3f244e" alt=""><figcaption></figcaption></figure></div>

### 9. Deploy the function

<div align="left"><figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2Fmp2wYEJO28jxC9ySUVFQ%2F9.png?alt=media&amp;token=6476fdcd-8da9-4e87-8c31-efd94c6b1787" alt=""><figcaption></figcaption></figure></div>

### 10. Set function invocation privacy permissions

This allows any device with your endpoint URL the ability to verify purchases made via your app

1. Go to <https://console.cloud.google.com/functions/list>
2. Select the function you want to make public. (the function you just created)
3. Click the **Permissions** tab.
4. Click **Add Principle**
5. In the **Add members** field, type `allUsers`
6. Select the **Cloud Function Invoker** role from the **Select a role** drop-down menu.
7. Click **Add**.
