• Blog
  • Documentation
  • Courses
  • Changelog
  • AI Starters
  • UI Kit
  • FAQ
  • Supamode
    New
  • Pricing

Launch your next SaaS in record time with Makerkit, a React SaaS Boilerplate for Next.js and Supabase.

Makerkit is a product of Makerkit Pte Ltd (registered in the Republic of Singapore)Company Registration No: 202407149CFor support or inquiries, please contact us

About
  • FAQ
  • Contact
  • Verify your Discord
  • Consultation
  • Open Source
  • Become an Affiliate
Product
  • Documentation
  • Blog
  • Changelog
  • UI Blocks
  • Figma UI Kit
  • AI SaaS Starters
License
  • Activate License
  • Upgrade License
  • Invite Member
Legal
  • Terms of License
    • Adding API Routes
    • Change Authentication strategy
    • Fetching the signed in User
    • Reading a Document
    • Creating a Document
    • Configuring Plans
    • Project Configuration
    • Updating the Navigation menu
    • Adding a new translation string
    • Guarding an API Route
    • Adding Pages
    • Updating the Sidebar menu
    • Require Email Verification
    • Fetching the selected Organization
    • Reading a list of Documents
    • Updating a Document
    • Running the Stripe Webhook locally
    • Branding
    • Setting a Default Language
    • Dark Theme
    • Theming
    • Calling API Routes from the client
    • Deleting a Document
    • Updating the Logo
    • Adding a new language in the Next.js Firebase SaaS Kit
    • Checking CSRF Tokens
    • Passing data from server to client
    • Updating the Fonts
    • Adding Pages
    • Guarding Pages
    • Using Lemon Squeezy instead of Stripe
    • Updating the Favicons
    • Using the Language Switcher
    • Environment variables
    • Detect current Locale
    • Setting up Emails

Creating a Document

In this tutorial we show how you can create a document in Firestore using the Web and Admin SDKs.

Creating a document in Firestore can be achieved using both the Web (Client) and the Admin SDK (Server). Depending on the use-case, you will use one or the other.

  1. Client SDK: When you want to create a document from the browser, you will use the Client SDK. This is the most common use-case, and can be done when the user has the permissions required to create a document based on their security rules.
  2. Admin SDK: When you want to create a document from a server, you will use the Admin SDK. This is useful when you want to create a document from a server, or when you want to create a document that the user does not have permission to create.

Client SDK

To create a document, we will use the addDoc function from the Web Firebase SDK.

We can use hooks to make sure we have access to the Firestore instance, and the collection we want to add the document to.

tsx
import { useFirestore } from 'reactfire';
import { useCallback } from 'react';
import { addDoc, collection } from 'firebase/firestore';
function useCreateTask() {
const firestore = useFirestore();
const tasksCollection = collection(firestore, `/tasks`);
return useCallback(
(task: {
title: string;
description: string;
completed: boolean;
}) => {
return addDoc(tasksCollection, task);
},
[tasksCollection]
);
}

We can then use this hook in our component to create a new task.

tsx
import { useCallback } from "react";
const CreateTaskForm = () => {
const createTask = useCreateTask();
// ... other code
const onSubmit = useCallback(async (task: {
title: string;
description: string;
completed: boolean;
}) => {
await createTask(task);
}, [createTask]);
// ... other code
};

Admin SDK

To create a document, we will use the add function from the Admin Firebase SDK.

We can use the Admin SDK only in API Route handlers and the getServerSideProps function. For two reasons:

  1. The Admin SDK is for Node.js environments, and cannot be used in the browser.
  2. The Admin SDK requires a service account, which should not be exposed to the browser.
tsx
import getRestFirestore from '~/core/firebase/admin/get-rest-firestore';
async function addTaskFromServer(task: {
title: string;
description: string;
completed: boolean;
}) {
const firestore = getRestFirestore();
const tasksCollection = firestore.collection(firestore, `tasks`);
await tasksCollection.add(task);
return task;
}

You can now use the function addTaskFromServer in your API Route handlers and getServerSideProps functions.

On this page
  1. Client SDK
    1. Admin SDK