Put data to Directus REST API (authentication?)

Hi! My first post here, and my first steps with n8n (please excuse my overall lacking knowledge and language misconceptions in advance!). Trying to implement my first workflow, part of it seems to be working already: I have n8n running in a Docker container in our intranet, where a I can pull a CSV-File from a webserver with HTTP Request node. Each row in the CSV is supposed to be a psql row in Directus (REST API Items: Data Access | Directus Docs). The data flows to a Spreadsheet File node, and I can see the resulting JSON data, which should be the post data flowing to the Directus API. Now there I am hitting the ceiling without the slightest clue how the Directus authentication with tokens (Authentication | Directus Docs) is going to be implementet in n8n…

What is the next node behind the Spreadsheet File node? I guess a HTTP Request node, but I have no idea what that basic/digest/header auth etc. options are nor how to set that up. Any hints?

Hi @srob41,

Looking at that authentication document you should be able to get away with Header Auth as an option in the HTTP Request Node. So the Credentials name would be something you will remember to reference it later in the node. For Name but Authorization and for Value put Bearer eyJh...KmUk.

A bit further down in that documentation it mentions a user token which is handy for server to server communications, That is what I would probably use.

Hopefully this helps :+1:

1 Like

Hey @srob41!

Welcome to the community :sparkling_heart:

You’re correct. The next node would be the HTTP Request node. As @jon mentioned you can use the Header Auth in the node.

@shrey-42 is using n8n and Directus together. Maybe he can share some more information :slight_smile:

2 Likes

@srob41 As Jon mentioned, in the HTTP node, you should:

  1. Select Authentication: ‘Header Auth’
  2. Credentials > Create New , like so:

Note: In case it’s not known, YOUR-STATIC-TOKEN can be obtained from:
Directus app > User’s Profile > Token

3 Likes

Hi all, thanks for taking care! I’m getting closer, but not there yet… Created a static token in Directus, which is working as query parameter as well as Header Auth as described by shrey-42. I set headers for Content-Type and Cache-Control.

When I execute the workflow, success is reported, but all JSON items from the CSV import produce empty rows in the directus database, all fields contain null. The response of the HTTP Request node looks like this (example for on row):

{
"data": {
"id": 81,
"organisationseinheit": null,
"bereich_in_org": null,
"vorname": null,
"email": null,
"vertretung": null,
"strasse": null,
"last_update": null,
"firma": null,
"nachname": null,
"schlagworte": null,
"telefon2": null,
"guid": null,
"telefon1": null,
"zustaendigkeit": null,
"ort": null,
"amtsnummer": null,
"mobiltelefon": null,
"fax_neu": null
}
},

The output of the Spreadsheet File node in front of the HTTP Request for DB insertion looks like this:

{
"nachname": "some-name",
"vorname": "Tanja",
"telefon1": 11200,
"telefon2": "/",
"mobiltelefon": "/",
"amtsnummer": "12341234-1200",
"fax_neu": "1234134-1217",
"email": "[email protected]",
"zustaendigkeit": "Stufenleitung Mitelstufen Fortbildungsbeauftragte/r",
"vertretung": "some-other-name",
"ort": "some-where",
"firma": "some-company",
"strasse": "some-street",
"organisationseinheit": "some-schule",
"bereich_in_org": "Mittelstufe 3",
"last_update": "17.02.2021 15:41:37",
"guid": "some-f3a-a99e-4930-9c05-c049e1783f07"
},

If I try to insert a row via fetch from javascript/sveltekit, with all the same parameters and headers, the content of the fields will be in the DB as wanted.

Hey @srob41!

I am glad that you were able to connect n8n with Directus :slight_smile:

Can you share your workflow here? Make sure to replace any private information (eg. your credentials) with some dummy data.

I guess the workflow is what I get by clicking on “Download” in the n8n app? It is this:

{
  "name": "TL_Import1",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "url": "=http://10.34.2.180:8080/xfer/tl-import1c_short.csv",
        "responseFormat": "file",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        500,
        550
      ]
    },
    {
      "parameters": {
        "options": {
          "includeEmptyCells": false,
          "readAsString": false
        }
      },
      "name": "Spreadsheet File",
      "type": "n8n-nodes-base.spreadsheetFile",
      "typeVersion": 1,
      "position": [
        650,
        550
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "http://10.34.2.182:8056/items/test2",
        "options": {},
        "headerParametersUi": {
          "parameter": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Cache-Control",
              "value": "no-cache"
            }
          ]
        }
      },
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        850,
        550
      ],
      "credentials": {
        "httpHeaderAuth": "Directus Auth"
      }
    }
  ],
  "connections": {
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Spreadsheet File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spreadsheet File": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": 1
}

This is all I got, didn’t remove anything. The credentials data I set up according to shrey-42 is not in the JSON data, but I can see it on the nodes panel.

Because rows are inserted to the DB in Directus, I guess that the authentication part is working…

What about the start node? Do I need that, or can I delete it?

Thanks for sharing! The Start node is the default node, and unless you’re using any trigger node, you should use the Start node.

I looked at your workflow, and it seems like you’re not passing body parameters. You need to pass on the data in the body parameters. I’ve created a small workflow that might be helpful. Since I don’t have access to your server, I am using mock data. If you open the HTTP Request1 node, you will notice that I’ve added the data I want to add in the body parameters and referenced its value from the previous node. This will add the values for the fields vorname and nachname.

{
  "nodes": [
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "http://10.34.2.182:8056/items/test2",
        "options": {},
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "nachname",
              "value": "={{$json[\"nachname\"]}}"
            },
            {
              "name": "vorname",
              "value": "={{$json[\"vorname\"]}}"
            }
          ]
        },
        "headerParametersUi": {
          "parameter": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Cache-Control",
              "value": "no-cache"
            }
          ]
        }
      },
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        750,
        550
      ],
      "credentials": {
        "httpHeaderAuth": "Directus Auth"
      }
    },
    {
      "parameters": {
        "functionCode": "return [\n  {json:{\n\"nachname\": \"some-name\",\n\"vorname\": \"Tanja\",\n\"telefon1\": 11200,\n\"telefon2\": \"/\",\n\"mobiltelefon\": \"/\",\n\"amtsnummer\": \"12341234-1200\",\n\"fax_neu\": \"1234134-1217\",\n\"email\": \"[email protected]\",\n\"zustaendigkeit\": \"Stufenleitung Mitelstufen Fortbildungsbeauftragte/r\",\n\"vertretung\": \"some-other-name\",\n\"ort\": \"some-where\",\n\"firma\": \"some-company\",\n\"strasse\": \"some-street\",\n\"organisationseinheit\": \"some-schule\",\n\"bereich_in_org\": \"Mittelstufe 3\",\n\"last_update\": \"17.02.2021 15:41:37\",\n\"guid\": \"some-f3a-a99e-4930-9c05-c049e1783f07\"\n}}\n]"
      },
      "name": "mock data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        550
      ]
    }
  ],
  "connections": {
    "mock data": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Ah, I see! Now my workflow is flowing, have my db filled. Thanks a lot!

I had to enter all the body parameters by hand (did it in my vscode and copied the workflow json to the Editor UI). Since in my case the database field names are identical to the keys in the JSON data input, I wonder if that mapping process could be automated with n8n nodes… I found that I can use a function item to iterate through the item keys of the JSON data, constructing the JSON part for the body parameters like I did manually:

        "bodyParametersUi": {
          "parameter": [
            { "name": "=nachname", "value": "={{$json[\"nachname\"]}}" },
            { "name": "=vorname", "value": "={{$json[\"vorname\"]}}" },
            { "name": "=telefon1", "value": "={{$json[\"telefon1\"]}}" },
            { "name": "=telefon2", "value": "={{$json[\"telefon2\"]}}" },
            { "name": "=mobiltelefon", "value": "={{$json[\"mobiltelefon\"]}}" },
            { "name": "=amtsnummer", "value": "={{$json[\"amtsnummer\"]}}" },
            { "name": "=fax_neu", "value": "={{$json[\"fax_neu\"]}}" },
            { "name": "=email",          "value": "={{$json[\"email\"]}}" },
            { "name": "=zustaendigkeit", "value": "={{$json[\"zustaendigkeit\"]}}" },
            { "name": "=vertretung", "value": "={{$json[\"vertretung\"]}}" },
            { "name": "=ort", "value": "={{$json[\"ort\"]}}" },
            { "name": "=firma", "value": "={{$json[\"firma\"]}}" },
            { "name": "=strasse", "value": "={{$json[\"strasse\"]}}" },
            { "name": "=organisationseinheit", "value": "={{$json[\"organisationseinheit\"]}}" },
            { "name": "=bereich_in_org", "value": "={{$json[\"bereich_in_org\"]}}" },
            { "name": "=schlagworte", "value": "={{$json[\"schlagworte\"]}}" },
            { "name": "=last_update", "value": "={{$json[\"last_update\"]}}" },
            { "name": "=guid", "value": "={{$json[\"guid\"]}}" }
          ]
        },

But how would I transmit that into a subsequent HTTP Request to execute the API db insert for that item? I guess there is a n8n practice to solve such a task…

Hey @srob41!

I am not sure if I understood your question correctly. Do you want to create the JSON in the Function Item node and then pass it on to the HTTP Request node? In that case you would still have to map either all the fields individually, or pass the change the parameters in the HTTP Request node so that you can send the JSON from the previous node.

The solution that I’ve suggested is used for all the databases in n8n. We handle the mapping under the hood in the native nodes of this databases. Since you’re using the HTTP Request node, you will have to handle it in the node.

Ok, get it. Will dive deeper into that stuff later. In the first place I needed a workflow to fill my databases, and will leave n8n aside for some time. But I am really fascinated by what might be done with n8n in the scattered software landscape of our NGO. Guess I will have more questions in the future…

tyvm