Set Workflow Tags via API

Is it possible to define a tag (or array of tags) for a workflow via the REST API, either via POST or PUT endpoints?

4 Likes

Hello @AndrewMitchell,

For that you can use the internal API, not the public one. But I don’t recommend it as I imagine that the internal API can change without notice in some new version of the product.

So I would move this topic to the Feature Requests category to ask for include the Tags Management functionality to the Public API.

Anyway, here is how you can create tags and set tags to a workflow using the internal REST API:

You first need to login:

curl --location --request POST 'http://localhost:5678/rest/login' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email": "[email protected]",
    "password": "User.12345"
}'

You will get in the response a Cookie like this one:

Set-Cookie: n8n-auth=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImY5Y2IwZmJhLTBjYWUtNDY0OC04YjA1LWNjMWI4NzQ1NmRjYSIsImVtYWlsIjoidXNlckB1c2VyLmNvbSIsInBhc3N3b3JkIjoiMDAyMzZiNDc4MjVkZmZhYjgzYTU4NzM5OTllMzI2MmM4N2QxZmU0NzAxZGQwODVlNTljMTY3Y2NmMzg2OTNjOSIsImlhdCI6MTY3MjQ3Nzk0MywiZXhwIjoxNjczMDgyNzQzfQ.HgP-uFrpmGBTPTX92GUYl3cKYiHo12l9q23C26p63EY; Max-Age=604800; Path=/; Expires=Sat, 07 Jan 2023 09:12:23 GMT; HttpOnly; SameSite=Lax

Now you can use that Cookie to make the other calls. For example, to create a Tag:

curl --location --request POST 'http://localhost:5678/rest/tags' \
--header 'Cookie: n8n-auth=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImY5Y2IwZmJhLTBjYWUtNDY0OC04YjA1LWNjMWI4NzQ1NmRjYSIsImVtYWlsIjoidXNlckB1c2VyLmNvbSIsInBhc3N3b3JkIjoiMDAyMzZiNDc4MjVkZmZhYjgzYTU4NzM5OTllMzI2MmM4N2QxZmU0NzAxZGQwODVlNTljMTY3Y2NmMzg2OTNjOSIsImlhdCI6MTY3MjQ3Nzk0MywiZXhwIjoxNjczMDgyNzQzfQ.HgP-uFrpmGBTPTX92GUYl3cKYiHo12l9q23C26p63EY; n8n-auth=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImY5Y2IwZmJhLTBjYWUtNDY0OC04YjA1LWNjMWI4NzQ1NmRjYSIsImVtYWlsIjoidXNlckB1c2VyLmNvbSIsInBhc3N3b3JkIjoiMDAyMzZiNDc4MjVkZmZhYjgzYTU4NzM5OTllMzI2MmM4N2QxZmU0NzAxZGQwODVlNTljMTY3Y2NmMzg2OTNjOSIsImlhdCI6MTY3MjQ3Nzk0MywiZXhwIjoxNjczMDgyNzQzfQ.HgP-uFrpmGBTPTX92GUYl3cKYiHo12l9q23C26p63EY' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "Some Tag"
}'

In the response you will get the ID of the tag:

{
    "data": {
        "name": "Some Tag",
        "createdAt": "2022-12-31T09:13:08.401Z",
        "updatedAt": "2022-12-31T09:13:08.401Z",
        "id": "3"
    }
}

Then you can add the tag to any workflow with an arry of ids:

curl --location --request PATCH 'http://localhost:5678/rest/workflows/1' \
--header 'Cookie: n8n-auth=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImY5Y2IwZmJhLTBjYWUtNDY0OC04YjA1LWNjMWI4NzQ1NmRjYSIsImVtYWlsIjoidXNlckB1c2VyLmNvbSIsInBhc3N3b3JkIjoiMDAyMzZiNDc4MjVkZmZhYjgzYTU4NzM5OTllMzI2MmM4N2QxZmU0NzAxZGQwODVlNTljMTY3Y2NmMzg2OTNjOSIsImlhdCI6MTY3MjQ3Nzk0MywiZXhwIjoxNjczMDgyNzQzfQ.HgP-uFrpmGBTPTX92GUYl3cKYiHo12l9q23C26p63EY; n8n-auth=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImY5Y2IwZmJhLTBjYWUtNDY0OC04YjA1LWNjMWI4NzQ1NmRjYSIsImVtYWlsIjoidXNlckB1c2VyLmNvbSIsInBhc3N3b3JkIjoiMDAyMzZiNDc4MjVkZmZhYjgzYTU4NzM5OTllMzI2MmM4N2QxZmU0NzAxZGQwODVlNTljMTY3Y2NmMzg2OTNjOSIsImlhdCI6MTY3MjQ3Nzk0MywiZXhwIjoxNjczMDgyNzQzfQ.HgP-uFrpmGBTPTX92GUYl3cKYiHo12l9q23C26p63EY' \
--header 'Content-Type: application/json' \
--data-raw '{
    "tags":["3"]
}'

And you have it:

Screenshot 2022-12-31 at 10.23.36

Above calls are for a self-hosted environment. If you are using a cloud environment, the login should be like this:

curl --location --request POST 'https://api.n8n.cloud/auth/login' \
--header 'Content-Type: application/json' \
--data-raw '{
    "username": "[email protected]",
    "password": "User.12345"
}'

As a response you will have a JSON that includes a token. Use that token in the next calls:

curl --location --request POST 'https://{yourAccount}.app.n8n.cloud/rest/tags' \
--header 'Cookie: token={someToken}' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "Some Tag"
}'

But again, I would use this just as a workaround while the functionality in the public API is implemented.

Hope this helps.

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.