> ## Documentation Index
> Fetch the complete documentation index at: https://docs.withleaf.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect to CNHI FieldOps API

> Connect CNHI FieldOps (Case IH, New Holland) data to Leaf. Create a developer application, complete the OAuth flow, and attach credentials to a Leaf user.

This tutorial walks through connecting CNHI FieldOps to Leaf so you can sync field boundaries, machine files, and field operations from Case IH and New Holland equipment. You'll create a developer application, run the OAuth flow to get user tokens, and attach provider credentials to a Leaf user.

<Tip>
  [Magic Link](/components/magic-link) and [Leaf Link](/components/leaf-link) handle the OAuth UI for you. This tutorial covers the manual flow for developers building it into their own application.
</Tip>

<Note>
  CNH Industrial has two API platforms. This tutorial covers **CNHI FieldOps** (the current platform). For the legacy AFS Connect API, see [Connect CNHI (AFS Connect)](/guides/tutorials/connect-cnhi).
</Note>

## Before you start

* A Leaf account with a valid API token.
* A Leaf user created.
* A CNH developer account registered with a **company-domain email** (Gmail, Hotmail, and other generic domains are not supported). Register at [develop.cnh.com](https://develop.cnh.com/).

## Step 1: Create a CNHI FieldOps application

1. Sign in to the [CNH developer portal](https://develop.cnh.com/) (or create an account if you don't have one).
2. Navigate to **Account**, then select **My Applications** in the left-hand menu.
3. Click **Add New Application**.
4. Fill out the Application Information form:
   * **Application Name** — a label for your integration (e.g., "My Company - Leaf Integration").
   * **Portal** — select **FieldsOps Portal**.
   * **Region** — select the region your growers operate in.
   * **OAuth Callback URL(s)** — enter the URL where CNHI should redirect growers after they authorize. If you're using **Magic Link or Leaf Link**, enter `https://widget.withleaf.io`. If you're building your own OAuth flow, enter your application's callback URL (the same URL you'll pass as `client_redirect_url` in Step 2).
   * Fill in the remaining required fields (description, icon, contact info) and click **Submit**.
5. Once the application is created, go to **My Applications**, click your application name, then find your **Client ID**, **Client Secret**, and **Subscription Key** in the API Information section.

<Warning>
  These credentials are specific to the FieldOps API. Existing CNHI (AFS Connect) credentials will not work. You must create a new application in the developer portal.
</Warning>

## Step 2: Get the authorization URL

Leaf provides a helper that constructs the CNHI FieldOps authorization URL:

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://cnhi-oauth2-helper.withleaf.io/fieldops/get_url" \
    -H "Content-Type: application/json" \
    -d '{
      "client_id": "your-cnhi-client-id",
      "client_redirect_url": "https://your-app.com/callback",
      "production": false
    }'
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      "https://cnhi-oauth2-helper.withleaf.io/fieldops/get_url",
      json={
          "client_id": "your-cnhi-client-id",
          "client_redirect_url": "https://your-app.com/callback",
          "production": False
      }
  )
  auth_url = response.json()["url"]
  ```

  ```javascript JavaScript theme={null}
  const res = await fetch("https://cnhi-oauth2-helper.withleaf.io/fieldops/get_url", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      client_id: "your-cnhi-client-id",
      client_redirect_url: "https://your-app.com/callback",
      production: false,
    }),
  });
  const { url } = await res.json();
  ```
</CodeGroup>

Set `production` to `false` for the CNHI staging environment (the default for new apps). Set it to `true` once your app is promoted to production.

<Warning>
  While in the CNHI staging environment, only staging test accounts work. Production customer accounts won't authenticate. The reverse is true in production.
</Warning>

Redirect the grower to the returned URL. After authorization, CNHI redirects to your `client_redirect_url` with a `code` in the URL.

## Step 3: Exchange the code for a refresh token

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://cnhi-oauth2-helper.withleaf.io/fieldops/get_token" \
    -H "Content-Type: application/json" \
    -d '{
      "client_id": "your-cnhi-client-id",
      "client_secret": "your-cnhi-client-secret",
      "response_url": "https://your-app.com/callback?code=abc123",
      "client_redirect_url": "https://your-app.com/callback"
    }'
  ```

  ```python Python theme={null}
  tokens = requests.post(
      "https://cnhi-oauth2-helper.withleaf.io/fieldops/get_token",
      json={
          "client_id": "your-cnhi-client-id",
          "client_secret": "your-cnhi-client-secret",
          "response_url": "https://your-app.com/callback?code=abc123",
          "client_redirect_url": "https://your-app.com/callback"
      }
  ).json()
  refresh_token = tokens["refresh_token"]
  ```

  ```javascript JavaScript theme={null}
  const tokenRes = await fetch(
    "https://cnhi-oauth2-helper.withleaf.io/fieldops/get_token",
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        client_id: "your-cnhi-client-id",
        client_secret: "your-cnhi-client-secret",
        response_url: "https://your-app.com/callback?code=abc123",
        client_redirect_url: "https://your-app.com/callback",
      }),
    }
  );
  const { refresh_token } = await tokenRes.json();
  ```
</CodeGroup>

The `response_url` is the full URL the grower was redirected to, including the `code` parameter that CNHI appended.

## Step 4: Attach credentials to the Leaf user

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST "https://api.withleaf.io/services/usermanagement/api/users/{leafUserId}/cnhi-field-ops-credentials" \
    -H "Authorization: Bearer YOUR_LEAF_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "clientId": "your-cnhi-client-id",
      "clientSecret": "your-cnhi-client-secret",
      "subscriptionKey": "your-cnhi-subscription-key",
      "refreshToken": "the-refresh-token-from-step-3",
      "clientEnvironment": "STAGE"
    }'
  ```

  ```python Python theme={null}
  response = requests.post(
      f"https://api.withleaf.io/services/usermanagement/api/users/{leaf_user_id}/cnhi-field-ops-credentials",
      headers={"Authorization": f"Bearer {leaf_token}"},
      json={
          "clientId": "your-cnhi-client-id",
          "clientSecret": "your-cnhi-client-secret",
          "subscriptionKey": "your-cnhi-subscription-key",
          "refreshToken": refresh_token,
          "clientEnvironment": "STAGE"
      }
  )
  print(response.json())
  ```

  ```javascript JavaScript theme={null}
  const res = await fetch(
    `https://api.withleaf.io/services/usermanagement/api/users/${leafUserId}/cnhi-field-ops-credentials`,
    {
      method: "POST",
      headers: {
        Authorization: `Bearer ${leafToken}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        clientId: "your-cnhi-client-id",
        clientSecret: "your-cnhi-client-secret",
        subscriptionKey: "your-cnhi-subscription-key",
        refreshToken: refresh_token,
        clientEnvironment: "STAGE",
      }),
    }
  );
  console.log(await res.json());
  ```
</CodeGroup>

Set `clientEnvironment` to `PRODUCTION` once your CNHI FieldOps application is promoted to production. Leaf handles token refresh automatically.

## Step 5: Confirm the credentials are attached

```bash theme={null}
curl "https://api.withleaf.io/services/usermanagement/api/users/{leafUserId}/cnhi-field-ops-credentials" \
  -H "Authorization: Bearer YOUR_LEAF_TOKEN"
```

If this worked, Leaf returns the CNHI FieldOps credential object for the Leaf user.

## What you built

You connected CNHI FieldOps to a Leaf user. Leaf now syncs field boundaries, machine files, and field operations from Case IH and New Holland equipment. Query the data through the [field operations](/guides/tutorials/field-operations-quickstart) endpoints.

For the credentials schema and management endpoints, see the [CNHI FieldOps provider guide](/providers/cnhi-fieldops) and the [provider credentials API reference](/api-reference/providers).
