Directus icon

Directus

Consume Directus API

Overview

The Directus - Relation: Create operation in this n8n node allows you to create a new relation between collections in a Directus instance. Relations in Directus define how different collections (tables) are linked, such as one-to-one, one-to-many, or many-to-many relationships. This is particularly useful for structuring complex data models where entities need to be connected.

Common scenarios:

  • Setting up a new relationship between two collections, e.g., linking articles to authors.
  • Automating the creation of relations during project setup or migrations.
  • Integrating with other systems that require dynamic schema changes.

Practical example:
You want to relate an articles collection to a directus_files collection via a featured_image field. You can use this operation to programmatically define that relationship.


Properties

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

Output

  • The output will be a JSON object representing the created relation as returned by the Directus API.
  • The structure typically includes all properties of the created relation, such as:
    • collection: The name of the source collection.
    • field: The field used for the relation.
    • related_collection: The target collection.
    • Other metadata about the relation (type, schema, etc.).

Example output:

{
  "collection": "articles",
  "field": "featured_image",
  "related_collection": "directus_files",
  "meta": { /* ... */ },
  // ...other relation properties
}

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 may return errors if required properties (like collection, field, or related_collection) are missing from the input.
  • Permission denied: The API credentials used must have permission to manage schema/relations in Directus.

Error messages:

  • "Unexpected token ...": Indicates malformed JSON in the Data (JSON) property.
  • "Missing required property ...": Indicates a required field is missing in your relation object.
  • "403 Forbidden" or similar: Your API user does not have sufficient permissions.

Links and References

Discussion