title: How can I create and run a transaction that writes to two Firestore?
body: <p>I have two applications, each belonging to a different firebase project.<br />
  I want to write to both Firestore with the same transaction.<br />
  How can I do this?</p>
  <pre class="lang-js prettyprint-override"><code>import admin from &quot;firebase-admin&quot;;
  
  const primaryFirebase = admin.initializeApp();
  const secondaryFirebase = admin.initializeApp({ credential }, &quot;secondary&quot;);
  
  const primaryFirestore = primaryFirebase.firestore();
  const secondaryFirestore = secondaryFirebase.firestore();
  
  const bookId = &quot;&lt;book ID&gt;&quot;;
  const comment = { body: &quot;some text&quot; };
  
  const primaryBookReference = primaryFirestore.collection(&quot;book&quot;).doc(bookId);
  if ((await primaryBookReference.get()).exists) return;
  
  const secondaryBookReference = secondaryFirestore.collection(&quot;book&quot;).doc(bookId);
  if ((await secondaryBookReference.get()).exists) return;
  
  // TODO: same transaction
  await primaryBookReference.collection(&quot;comment&quot;).add(comment);
  await secondaryBookReference.collection(&quot;comment&quot;).add(comment);
  </code></pre>