Create a File via GraphQL

In this guide, you will create a Guardrails File programmatically using the createResource GraphQL mutation. This approach is ideal for automation workflows, triggered by CI/CD pipelines, Lambda functions, or scripts that pull data from external systems.

Prerequisites

Step 1: Write the Mutation

The createResource mutation creates any Guardrails resource, including files. Set the type field to the file resource type URI.

mutation CreateFile($input: CreateResourceInput!) {
  createResource(input: $input) {
    data
    metadata
    turbot {
      id
      akas
    }
    trunk {
      title
    }
  }
}

Step 2: Define the Variables

The variables specify the file's parent, type, alias, content, and metadata.

{
  "input": {
    "parent": "tmod:@turbot/turbot#/",
    "type": "tmod:@turbot/turbot#/resource/types/file",
    "akas": ["environment_map"],
    "data": {
      "accounts": {
        "123456789012": { "environment": "production", "team": "payments" },
        "234567890123": { "environment": "staging", "team": "payments" },
        "345678901234": { "environment": "development", "team": "fintech" }
      }
    },
    "metadata": {
      "title": "Environment Map",
      "description": "Maps AWS account IDs to environment and team metadata"
    }
  }
}
FieldDescription
parentThe parent resource ID or URI. Use tmod:@turbot/turbot#/ for the workspace root, or a folder's numeric ID or AKA.
typeMust be tmod:@turbot/turbot#/resource/types/file.
akasAn array of unique aliases. Policies reference the file by this name.
dataAny valid JSON object. This is the file's content.
metadata.titleThe file's display name.
metadata.descriptionOptional description.

Step 3: Execute with curl

curl -s -X POST https://YOUR_WORKSPACE.cloud.turbot.com/api/latest/graphql \
  -H "Authorization: Basic $(echo -n 'ACCESS_KEY_ID:SECRET_ACCESS_KEY' | base64)" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "mutation CreateFile($input: CreateResourceInput!) { createResource(input: $input) { data turbot { id akas } } }",
    "variables": {
      "input": {
        "parent": "tmod:@turbot/turbot#/",
        "type": "tmod:@turbot/turbot#/resource/types/file",
        "akas": ["environment_map"],
        "data": {
          "accounts": {
            "123456789012": { "environment": "production", "team": "payments" }
          }
        },
        "metadata": {
          "title": "Environment Map",
          "description": "Maps AWS account IDs to environment and team metadata"
        }
      }
    }
  }'

Replace YOUR_WORKSPACE, ACCESS_KEY_ID, and SECRET_ACCESS_KEY with your workspace URL and credentials.

Update an Existing File

To update a file's data, use the updateResource mutation with the file's ID or AKA:

mutation UpdateFile($input: UpdateResourceInput!) {
  updateResource(input: $input) {
    data
    turbot {
      id
    }
  }
}
{
  "input": {
    "id": "environment_map",
    "data": {
      "accounts": {
        "123456789012": { "environment": "production", "team": "payments" },
        "234567890123": { "environment": "staging", "team": "payments" },
        "345678901234": { "environment": "development", "team": "fintech" },
        "456789012345": { "environment": "production", "team": "risk-analytics" }
      }
    }
  }
}

When you update a file, Guardrails automatically re-evaluates all calculated policies that reference it.

Troubleshooting

IssueSolution
401 UnauthorizedVerify your access key and secret are correct. Check the key has Turbot/Admin permissions.
"AKA already exists"Use updateResource instead of createResource to modify an existing file.
"Parent not found"Verify the parent resource ID exists. Use tmod:@turbot/turbot#/ for the workspace root.
Further assistanceOpen a Support Ticket

Next Steps