{data.title}
\n {props.preview &&You are currently in edit mode.
}\n {!props.preview && (\n\n To edit this site, go to login\n
\n )}\n{"pageProps":{"error":null,"preview":false,"file":{"sha":"","fileRelativePath":"content/tina-cloud/docs/guides/setup-tina-cloud-client.md","data":{"frontmatter":{"title":"Setup Tina Cloud Client","id":"/tina-cloud/docs/guides/setup-tina-cloud-client","prev":"/tina-cloud/docs/guides/setup-tina-cloud-client","next":null},"excerpt":" Prerequisite This guide gets the client working on a fresh NextJS site but can also be used as a reference to add the client to an existing site. To start with a new site. you can create one with: or…","markdownBody":"\n## Prerequisite\n\nThis guide gets the client working on a fresh NextJS site but can also be used as a reference to add the client to an existing site.\n\nTo start with a new site. you can create one with:\n\n```bash\nnpx create-next-app\n```\n\nor\n\n```bash\nyarn create next-app\n```\n\nGoing forward `yarn` with be used for examples.\n\nName it whatever you like and `cd` in your new project.\n\n### Add Typescript\n\nFor this guide we will be using Typescript. Add it to the project with this:\n\n```bash\nyarn add --dev typescript @types/react @types/react-dom @types/node\n```\n\nThen replace `_app.js` with `_app.tsx` and `index.js` with `index.tsx`.\n\nRun:\n\n```bash\ntouch tsconfig.json\n```\n\nAnd finally run:\n\n```bash\nyarn dev\n```\n\nThat should generate the tsconfig.json content required.\n\n## Install the client package\n\nThis package provides you with:\n\n- A `Client` class (which you can use as a TinaCMS API Plugin), that takes care of all interaction with the GraphQL server.\n- A `useForm` hook, that you can use to hook into the Tina forms that let you edit your content.\n\n```bash\nyarn add tina-graphql-gateway\n```\n\n_Choose the latest version_\n\n## Install CLI package\n\nYou'll also likely want to install our CLI to help with development:\n\n```bash\nyarn add --dev tina-graphql-gateway-cli\n```\n\n_Choose the latest version_\n\nThis CLI performs a few functions:\n\n- Generates GraphQL queries (and optionally TypeScript types) based on your content's schema.\n- Auditing your content's schema and checking for errors.\n- Running a GraphQL server using the built-in filesystem adapter.\n\nFor full documentation of the CLI, see [here](/tina-cloud/docs/reference/tina-cli).\n\n## Implementation\n\nWe'll now show how to use this package in a NextJS site\n\nNote: This solution relies on a long-running server, and will not work with NextJS' serverless [build target](https://nextjs.org/docs/api-reference/next.config.js/build-target).\n\n### Create Example Content\n\nLet's start by creating a simple piece of content. Our goal will to be able to access and change this content through an auto-generated GraphQL API and Tina forms.\n\nFirst create a new folder at the root of the project called `content`, then a subfolder of `content` called `pages`, and in there create a markdown file called `index.md`.\n\nIn `content/pages/index.md`, add this:\n\n```md\n---\ntitle: A great sight\n---\n```\n\n### Configuration\n\nBefore we can define the schema of our content, we need set up some configuration. Create a `.tina` directory at the project root and then create the following files.\n\n**.tina/settings.yml**\n\n```yml\n---\nnew_page_extension: md\nauto_deploy: false\nadmin_path:\nwebhook_url:\nsections:\n - type: directory\n path: content/pages # replace this with the relative path to your content section\n label: Pages\n create: documents\n match: '*.md'\n new_doc_ext: md\n templates:\n - index # replace this with your template filename name\nupload_dir: public/uploads\npublic_path: '/uploads'\nfront_matter_path: ''\nuse_front_matter_path: false\nfile_template: ':filename:'\n```\n\nThese [files](/tina-cloud/docs/reference/config-files/index) will create a map to our content to content models. In the above settings file, we declare any markdown files in our project should be a \"index\" type (we'll define this index type next).\n\n### Define Content Schema\n\n[Templates](/tina-cloud/docs/reference/config-files/front-matter-templates) define the shape of different content models.\n\n**.tina/front_matter/templates/index.yml**\n\n```yml\n---\nlabel: Index\nhide_body: false\ndisplay_field: title\nfields:\n - name: title\n type: text\n config:\n required: false\n label: Title\npages:\n - content/pages/index.md # This keeps reference to all the pages using this template\n```\n\n## Sourcing your content\n\n### Store the project in a GitHub repo\n\n_If you're following this guide with an existing project, make sure it is stored in GitHub and .tina has been pushed up._\n\nIf you have created a new NextJS site, then please now go and create a repository in GitHub and push up the project to it.\n\n### Create a Tina Cloud \"app\"\n\nOnce you have you have a repository, it is time to create a Tina Cloud \"app\". For help doing so, you can checkout [this guide](/tina-cloud/docs/guides/setup-tina-cloud-app).\n\nOnce you have a Tina Cloud \"app\", add a `.env` file to the root of your project. It should look like this:\n\n```\nNEXT_PUBLIC_REALM_NAME=YOUR ORGANIZATION ID\nNEXT_PUBLIC_TINA_CLIENT_ID=YOUR APP'S CLIENT ID\n```\n\nFill out the environment variables with the values you've recieved by creating your app.\n\n#### Using the data within our Next.JS site\n\nFirst, install the TinaCMS dependencies:\n\n```bash\nyarn add tinacms styled-components\n```\n\nIn `_app.tsx`, add TinaCMS, register the `Client`, and wrap our main layout in the `TinaCloudProvider` to support authentication, like so:\n\n**\\_app.tsx**\n\n```tsx\nimport React from 'react'\nimport { TinaProvider, TinaCMS } from 'tinacms'\nimport { TinaCloudProvider } from 'tina-graphql-gateway'\nimport createClient from '../components/client'\n\nfunction App({ Component, pageProps }) {\n return (\n
\n You are logged in to Tina.io. Return to homepage\n
\n )}\n >\n )\n}\n\nexport const getStaticProps = async ({ preview }) => {\n return {\n props: {\n preview: !!preview,\n },\n }\n}\n```\n\nAt this point, if you go to [/login](http://localhost:3000/login) you should now be able to login, for no good reason though because there's nothing yet to edit. Let's fix that next.\n\n## Editing your content\n\nLet's rejig `index.tsx` a bit:\n\n```tsx\nimport Head from 'next/head'\nimport styles from '../styles/Home.module.css'\nimport Cookies from 'cookies'\nimport createClient from '../components/client'\nimport { useForm, useTinaAuthRedirect } from 'tina-graphql-gateway'\nimport { DocumentUnion, Index as IndexResponse } from '../.tina/types'\n\nexport default function Home(props) {\n useTinaAuthRedirect()\n\n const data = props.preview\n ? useFormYou are currently in edit mode.
}\n {!props.preview && (\n\n To edit this site, go to login\n
\n )}\n