Directus (DrWade) icon

Directus (DrWade)

Consume Directus API

Overview

The Directus (DrWade) - Roles: Create Multiple operation allows you to create multiple role objects in a Directus instance in a single API call. This is particularly useful for administrators or developers who need to set up several roles at once, such as when initializing a new project, migrating from another system, or automating user management workflows.

Practical examples:

  • Bulk creation of standard roles like "Admin", "Editor", "Viewer", etc., during initial setup.
  • Importing roles from an external configuration or template.
  • Automating the provisioning of roles as part of a CI/CD pipeline for infrastructure-as-code scenarios.

Properties

Name Type Meaning
Data (JSON) json An array of partial role objects. Each object defines the properties of a role to be created, such as name, icon, description, and access flags.

Details:

  • The property expects a JSON array where each element represents a role to be created.
  • Example input:
    [
      {
        "name": "Interns",
        "icon": "verified_user",
        "description": null,
        "admin_access": false,
        "app_access": true
      },
      {
        "name": "Customers",
        "icon": "person",
        "description": null,
        "admin_access": false,
        "app_access": false
      }
    ]
    

Output

  • The output will be a single field named json containing the result of the bulk creation operation.
  • The structure of the output typically matches the response from the Directus API for the /roles POST endpoint, which is usually an array of the newly created role objects, each with their assigned IDs and all relevant properties.

Example output:

{
  "json": [
    {
      "id": "generated-role-id-1",
      "name": "Interns",
      "icon": "verified_user",
      "description": null,
      "admin_access": false,
      "app_access": true,
      ...
    },
    {
      "id": "generated-role-id-2",
      "name": "Customers",
      "icon": "person",
      "description": null,
      "admin_access": false,
      "app_access": false,
      ...
    }
  ]
}
  • If an error occurs and "Continue On Fail" is enabled, the output will contain an error field with the error message.

Dependencies

  • Directus API: Requires access to a running Directus instance.
  • API Credentials: You must configure the node with valid Directus API credentials (directusApi).
  • n8n Configuration: No special environment variables are required beyond standard n8n credential setup.

Troubleshooting

Common issues:

  • Invalid JSON: If the "Data (JSON)" property contains malformed JSON, the node will throw a parsing error. Ensure your input is valid JSON.
  • Missing Required Fields: Each role object may require certain fields (e.g., name). Omitting these can cause API errors.
  • Permission Denied: The provided API credentials must have permission to create roles. Otherwise, you'll receive an authorization error.
  • Partial Success: If some roles are created and others fail (e.g., due to duplicate names), only successful creations will appear unless "Continue On Fail" is enabled.

Error messages and resolutions:

  • "Unexpected token ... in JSON": Check your JSON syntax.
  • "Request failed with status code 403": Verify that your API credentials have sufficient permissions.
  • "Validation error: ...": Review the required fields for a role object in the Directus documentation.

Links and References

Discussion