This tutorial walks through connecting CNHi 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.
Magic Link and Leaf Link handle the OAuth UI for you. This tutorial covers the manual flow for developers building it into their own application.
Before you start
- A Leaf account with a valid API token.
- A Leaf user created.
- A CNHi developer account. Register at develop.cnh.com.
Step 1: Create a CNHi application
Sign in to the CNHi developer portal, go to Account Dashboard, and click Add Application.
After creating the application, go to App Registrations, click your application name, then API Information to find your:
- Client ID
- Client Secret
- Subscription Key
Step 2: Get the authorization URL
Leaf provides a helper that constructs the CNHi authorization URL:
curl -X POST "https://cnhi-oauth2-helper.withleaf.io/get_url" \
-H "Content-Type: application/json" \
-d '{
"client_id": "your-cnhi-client-id",
"client_secret": "your-cnhi-client-secret",
"subscription_key": "your-cnhi-subscription-key",
"client_redirect_url": "https://your-app.com/callback",
"production": false
}'
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.
While in the CNHi staging environment, only staging test accounts work. Production customer accounts won’t authenticate. The reverse is true in production.
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
curl -X POST "https://cnhi-oauth2-helper.withleaf.io/get_token" \
-H "Content-Type: application/json" \
-d '{
"client_id": "your-cnhi-client-id",
"client_secret": "your-cnhi-client-secret",
"subscription_key": "your-cnhi-subscription-key",
"response_url": "https://your-app.com/callback?code=abc123",
"client_redirect_url": "https://your-app.com/callback"
}'
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
curl -X POST "https://api.withleaf.io/services/usermanagement/api/users/{leafUserId}/cnhi-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"
}'
Set clientEnvironment to PRODUCTION once your CNHi application is promoted to production. Leaf handles token refresh automatically.
Step 5: Confirm the credentials are attached
curl "https://api.withleaf.io/services/usermanagement/api/users/{leafUserId}/cnhi-credentials" \
-H "Authorization: Bearer YOUR_LEAF_TOKEN"
If this worked, Leaf returns the CNHi credential object for the Leaf user.
What you built
You connected CNHi 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 endpoints.
For the credentials schema and management endpoints, see the CNHi provider guide and the provider credentials API reference.