> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://apidocs.polytomic.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://apidocs.polytomic.com/_mcp/server.

This example creates a Model Sync from BigQuery to Salesforce using the
Polytomic Terraform provider.

Prerequisites:

* Install the [Polytomic Terraform provider](https://registry.terraform.io/providers/polytomic/polytomic/latest).
* Set your Polytomic API key as an environment variable.

## 1. Configure the Polytomic provider

Configure the Polytomic provider in your Terraform script:

```terraform
terraform {
  required_providers {
    polytomic = {
      source  = "polytomic/polytomic"
      version = "~> 1.5"
    }
  }
}

provider "polytomic" {
    api_key = var.polytomic_api_key
}
```

Define `var.polytomic_api_key` in your variables file or pass it at apply time.

> 🤝 Partner keys
>
> If you are using a partner key, set `partner_key` on the provider and
> set `organization` on each resource that targets a specific organization.
>
> ```terraform
> provider "polytomic" {
>   partner_key = "<value from settings page>"
> }
> ```

## 2. Create a BigQuery Connection

Define a resource for the BigQuery connection:

```terraform
resource "polytomic_bigquery_connection" "user_data" {
  name = "BigQuery Users"

  configuration = {
    auth_method     = "service_account_key"
    service_account = file("bq.json")
  }
}
```

This example loads the service account key with the `file` function.
Any string value or variable works as well.

## 3. Create a Salesforce Connection

Polytomic supports several authorization modes for Salesforce. To
manage a Salesforce Connection from Terraform, you must supply the
client ID and secret.

If you want to use Polytomic's OAuth flow instead, create the
Connection through the admin console or the [authentication API](/guides/embedding-authentication),
then reference it from Terraform with the Salesforce datasource.

Define the Salesforce Connection:

```terraform

resource "polytomic_salesforce_connection" "sfdc" {
  name = "Salesforce Connection (TF)"
  configuration = {
    connect_mode        = "api"
    domain              = "my--sandbox.sandbox.my.salesforce.com"
    client_id           = var.sfdc_client_id
    client_secret       = var.sfdc_client_secret
    oauth_refresh_token = var.oauth_token
  }
}
```

> 🤝 Partner keys
>
> When using a partner key, you must also set the Organization ID on
> every resource.

## 4. Create a model over the user data

Define a model that queries BigQuery:

```terraform
resource "polytomic_model" "users" {
  name          = "Users"
  connection_id = polytomic_bigquery_connection.user_data.id
  configuration = jsonencode({
    dataset = "application"
    table   = "users"
  })
}
```

## 5. Create the Model Sync

Define a Model Sync to Salesforce. This example creates an update-only
sync to Salesforce Contacts, matched on email address:

```terraform
resource "polytomic_sync" "update_contacts" {
  name = "Update Contact Names"
  mode = "update"
  target = {
    connection_id = polytomic_salesforce_connection.sfdc.id
    object        = "Contact"
  }
  identity = {
    source = {
      model_id = polytomic_model.users.id
      field    = "email"
    }
    function = "Equality"
    target   = "Email"
  }
  active = false
  schedule = {
    frequency = "manual"
  }
  fields = [
    {
      source = {
        model_id = polytomic_model.users.id
        field    = "name"
      }
      target = "FirstName"
    },
  ]
}
```

## 6. Apply the configuration

Initialize Terraform and apply:

```shell
terraform init
terraform apply
```

Review the planned changes and confirm to proceed.

For more details, see the [Polytomic Terraform provider documentation](https://registry.terraform.io/providers/polytomic/polytomic/latest).