title: Firebase error writing to Firestore via a Function: "7 PERMISSION_DENIED: Missing or insufficient permissions"
body: <p>I'm trying to write a simple Firebase Function that adds a record to Firestore with TypeScript. The cut down version of the code is:</p>
  
  <pre><code>import * as admin from "firebase-admin";
  import * as functions from "firebase-functions";
  
  const config = functions.config();
  
  admin.initializeApp(functions.config().firebase);
  admin.database.enableLogging(true);
  
  exports.subscriptions = functions.https.onRequest(
    async (req, res) =&gt; {
      try {
        const requestSubscription = req.body;
        const subscription = {
          subscription_id: requestSubscription.subscription_id,
          email: requestSubscription.email || ""
        };
  
        const id = subscription.subscription_id;
        const dbSubscriptions = admin.firestore().collection("subscriptions");
        await dbSubscriptions.doc(id).set(subscription);
  
        return res.status(200).send(`Success`);
      } catch (e) {
        console.error(e.stack);
        return res.status(500).send(`Error`);
      }
    }
  );
  </code></pre>
  
  <p>This works as expected locally. When I deploy, I get this error:</p>
  
  <pre><code>Error: 7 PERMISSION_DENIED: Missing or insufficient permissions.
      at Object.exports.createStatusError (/user_code/node_modules/firebase-admin/node_modules/grpc/src/common.js:87:15)
      at Object.onReceiveStatus (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:1214:28)
      at InterceptingListener._callNext (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:590:42)
      at InterceptingListener.onReceiveStatus (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:640:8)
      at callback (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:867:24)
  </code></pre>
  
  <p>My functions/package.json file is:</p>
  
  <pre><code>{
    "name": "functions",
    "scripts": {
      "lint": "tslint --project tsconfig.json",
      "build": "tsc",
      "serve": "npm run build &amp;&amp; firebase serve --only functions",
      "shell": "npm run build &amp;&amp; firebase functions:shell",
      "start": "npm run shell",
      "deploy": "firebase deploy --only functions",
      "logs": "firebase functions:log"
    },
    "main": "lib/index.js",
    "dependencies": {
      "express": "4.16.3",
      "firebase-admin": "5.12.1",
      "firebase-functions": "1.0.3"
    },
    "devDependencies": {
      "tslint": "^5.8.0",
      "typescript": "^2.5.3"
    },
    "private": true
  }
  </code></pre>
  
  <p>The only link I'm getting searching for this error is this unanswered question:
  <a href="https://stackoverflow.com/questions/49910274/permission-denied-firestore-cloudfunction-typescript">PERMISSION_DENIED Firestore CloudFunction TypeScript</a></p>
  
  <p>I've set my Firestore rules to allow anyone to read and write as well even though the above is using admin and it still doesn't work. </p>
  
  <p>Can anyone help?</p>