Directus (DrWade) icon

Directus (DrWade)

Consume Directus API

Overview

The Directus (DrWade) n8n node for the resource Relations with the operation Create allows you to create a new relation between collections in a Directus project. This is useful when you want to define or automate the creation of relationships (such as one-to-one, one-to-many, or many-to-many) between different data collections within your Directus instance.

Common scenarios:

  • Automating the setup of new relations during project initialization.
  • Programmatically managing schema changes across environments.
  • Integrating Directus schema management into CI/CD pipelines.

Practical example:
Suppose you have an "articles" collection and a "directus_files" collection, and you want to relate an article's "featured_image" field to a file. This node can automate the creation of that relation.


Properties

Name Type Meaning
Data (JSON) json A partial relation object. This should include details such as the source collection, field, related collection, and other relation settings.

Output

  • The output will be a JSON object representing the created relation as returned by the Directus API.
  • The structure typically includes fields like collection, field, related_collection, and other metadata describing the relation.
  • Example output:
{
  "collection": "articles",
  "field": "featured_image",
  "related_collection": "directus_files",
  // ...other relation properties
}
  • 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 directusApi credentials in n8n for authentication.

Troubleshooting

Common issues:

  • Invalid JSON in Data (JSON): If the input is not valid JSON, the node will throw a parsing error. Ensure your JSON is correctly formatted.
  • Missing required fields: The Directus API expects certain fields in the relation object (e.g., collection, field, related_collection). Omitting these may result in API errors.
  • Insufficient permissions: The API key/user used must have permission to manage schema/relations in Directus.

Error messages:

  • "Unexpected token ... in JSON": Indicates malformed JSON in the Data (JSON) property.
  • "Request failed with status code 4xx/5xx": Indicates an issue with the Directus API request, such as missing fields or lack of permissions.

How to resolve:

  • Double-check your JSON syntax and required fields.
  • Verify your Directus user has appropriate permissions.
  • Check the Directus API documentation for the expected relation object structure.

Links and References

Discussion