- Código para mergear os cookies:
const oldCookieString = $('data').first().json.cookie || "";
const setCookieArray = $("get-setcookies").first().json.headers['set-cookie'] || [];
let cookieMap = {};
if (oldCookieString) {
oldCookieString.split(';').forEach(c => {
const [key, ...val] = c.trim().split('=');
if (key) cookieMap[key] = val.join('=');
});
}
setCookieArray.forEach(c => {
const cookiePart = c.split(';')[0];
const [key, ...val] = cookiePart.trim().split('=');
if (key) cookieMap[key] = val.join('=');
});
const newCookieString = Object.keys(cookieMap)
.map(key => `${key}=${cookieMap[key]}`)
.join('; ');
return { cookies_atualizados: newCookieString };Fluxo já implementado:
{
"name": "gerar link de afiliado",
"nodes": [
{
"parameters": {},
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [
-240,
304
],
"id": "55b6c605-98fa-4005-8a84-b112983855dc",
"name": "When clicking ‘Execute workflow’"
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "ddc5103f-1dd4-4385-afd8-b2b60e294224",
"name": "url",
"value": "={{ $('Webhook').item.json.body.url }}",
"type": "string"
},
{
"id": "355cfa2e-a52c-496d-9f4a-64c2dcda4834",
"name": "cookie",
"value": "={{ $json[\"cookies-mercadolivre\"] }}",
"type": "string"
}
]
},
"options": {}
},
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
208,
-16
],
"id": "f1d4f71f-bba6-4b93-b83d-1bf0b1686438",
"name": "data"
},
{
"parameters": {
"method": "POST",
"url": "https://www.mercadolivre.com.br/affiliate-program/api/v2/affiliates/createLink",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "accept",
"value": "application/json, text/plain, */*"
},
{
"name": "accept-language",
"value": "en-US,en;q=0.9"
},
{
"name": "device-memory",
"value": "8"
},
{
"name": "dnt",
"value": "1"
},
{
"name": "downlink",
"value": "10"
},
{
"name": "dpr",
"value": "1"
},
{
"name": "ect",
"value": "4g"
},
{
"name": "origin",
"value": "https://www.mercadolivre.com.br"
},
{
"name": "priority",
"value": "u=1, i"
},
{
"name": "referer",
"value": "https://www.mercadolivre.com.br/afiliados/linkbuilder"
},
{
"name": "rtt",
"value": "100"
},
{
"name": "sec-ch-ua",
"value": "\"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"144\", \"Microsoft Edge\";v=\"144\""
},
{
"name": "sec-ch-ua-mobile",
"value": "?0"
},
{
"name": "sec-ch-ua-platform",
"value": "\"macOS\""
},
{
"name": "sec-fetch-dest",
"value": "empty"
},
{
"name": "sec-fetch-mode",
"value": "cors"
},
{
"name": "sec-fetch-site",
"value": "same-origin"
},
{
"name": "user-agent",
"value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0"
},
{
"name": "viewport-width",
"value": "953"
},
{
"name": "x-csrf-token",
"value": "CRoUbkB5-udAuzRwZkidqm7yW9Omqcb1z1zs"
},
{
"name": "cookie",
"value": "={{ $json.cookies_atualizados }}"
}
]
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={\n \"urls\": [\n \"{{ $('data').item.json.url }}\"\n ],\n \"tag\": \"myshoplist\"\n}",
"options": {}
},
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.3,
"position": [
1104,
-16
],
"id": "0f798eca-9f7b-47f1-aa0e-0c6fb5515905",
"name": "HTTP Request"
},
{
"parameters": {
"operation": "get",
"propertyName": "cookies-mercadolivre",
"key": "cookies-mercadolivre",
"options": {}
},
"type": "n8n-nodes-base.redis",
"typeVersion": 1,
"position": [
-16,
-16
],
"id": "a6fe727c-86f2-4a6e-b819-5a2da04f2225",
"name": "get-cookies",
"credentials": {
"redis": {
"id": "e99faTHtS0aY2Rm7",
"name": "Redis account"
}
}
},
{
"parameters": {
"operation": "set",
"key": "cookies-mercadolivre",
"value": "_csrf=ydndjbaKNq7RaiotOBUN26Kd; ftid=8S2FjXEx6Nb6QY9pmCPMlUKlcjmUHwVR-1758568360278; _d2id=94838671-164b-40c9-b1e9-fafba61d2733; c_Zfwxvk=1; sc-menu-hide-new-section_MY_DETAILS=MY_DETAILS; main_domain=; main_attributes=; categories=; backend_dejavu_info=j%3A%7B%7D; sc-menu-hide-new-item_brand_registry=brand_registry; p_dsid=566acdda-b918-4bb4-91b2-3882aa15e835-1768318413855; _ml_ar-browser-check=1dfb6b30-7601-4833-98ef-2e6c200cc230; la-ttp-ftu=true; ml_gads_exit-popup-offer=2026-01-15T14:53:52.922Z; tooltip=true; ml_helpchat_csrf=lOfLGbUUBGQ8kyeBTDUJthVj; p_edsid=eb3ee7d7-2fa1-3672-9623-9cead28ca438-1768921023267; x-bf-session-v6=b87e184444314b82a44fbf7fbe0f341a; ml_cart-quantity=0; c_ui-navigation=6.8.0; c_Z1laz4H=1; modal-configuration={\"meli_usd_modal\":{\"view_cnt\":3,\"close_cnt\":0,\"view_time\":1769521208,\"close_time\":0}}; LAST_SEARCH=toalhas%20banhão%20grossas; g_state={\"i_l\":1,\"i_ll\":1769528019455,\"i_b\":\"ZfCiE6QwByQmvNAHmGtcexN2e/cBN+f+2KbhUy82RV0\",\"i_e\":{\"enable_itp_optimization\":3},\"i_p\":1768947622170}; ssid=ghy-012711-2z18qnmqTRsaHkdzzdYfa3aGoWrmqt-__-3147861829-__-1864222461436--RRR_0-RRR_0; orgnickp=FGBEACFHD95451; orguserid=0H00Tth7t07Z4; orguseridp=3147861829; cookiesPreferencesLoggedFallback=%7B%22userId%22%3A3147861829%2C%22categories%22%3A%7B%22advertising%22%3Atrue%2C%22functionality%22%3Atrue%2C%22performance%22%3Atrue%2C%22traceability%22%3Atrue%7D%7D; cookiesPreferencesNotLogged=%7B%22categories%22%3A%7B%22advertising%22%3Atrue%2C%22functionality%22%3Atrue%2C%22performance%22%3Atrue%2C%22traceability%22%3Atrue%7D%7D; cp=35500001; c_deals=11.64.0-rc-3; category=; last_query=apple%20watch; tooltips-configuration={\"compats_highlight_tooltip\":{\"view_cnt\":1,\"close_cnt\":0,\"view_time\":1769713247,\"close_time\":0}}; cookiesPreferencesLogged=%7B%22userId%22%3A3147861829%2C%22categories%22%3A%7B%22advertising%22%3Atrue%2C%22functionality%22%3Atrue%2C%22performance%22%3Atrue%2C%22traceability%22%3Atrue%7D%7D; _mldataSessionId=c6c5e3b9-d299-4686-88e3-54a890b94032; _snoopy=eyJmaW5nZXJwcmludCI6InhGcWhEM0x0L0dieUdqbzEramtBdktHdExBSFl5ME00TDNWazRCRWZwNXBEOWdDTTdXWS9nQ3p6SkpLd3l4VndmYzErR3FnYXZSYWZVRmxieHRBMlFkZTljV2JITzJqZkE1M3BHNXB3eit1a3djdnZNQ2FjR05oNy95R3Y5OHZJR2FMUnFVTDVGWVpRV1QvK0tIZjBuMkRBTmdEUWNVd2s4c2VLaCtMb0lGcDVqNEZZaDZnPSIsImtleSI6IittK1hFQ08xRjJDVDk3SEpCcGh1ZXBZdlRCcEtYWmkwSEl0UHVSbk9xS3ZlODVrU25rbzNLenJ2SGFaM0k4S2ZjYlQ3SXVKNlZ0bE5XelJRY0p6SFJHSjZabUdyR1ZhaCJ9; nsa_rotok=eyJhbGciOiJSUzI1NiIsImtpZCI6IjMiLCJ0eXAiOiJKV1QifQ.eyJpZGVudGlmaWVyIjoiOGYxYmMzNDUtZTEyYi00MjgxLWJiZTgtNWMzNjUxNDgyOGQzIiwicm90YXRpb25faWQiOiIzOTc4MDg2Zi00NmRmLTQwODYtOTRhMS1mYzZkN2ZiMzYzODEiLCJwbGF0Zm9ybSI6Ik1MIiwicm90YXRpb25fZGF0ZSI6MTc2OTc3MjE0OCwiZXhwIjoxNzcyMzYzNTQ4LCJqdGkiOiI3YTk4NTQ2My0zODM1LTQ4MjAtOTY0MC02NWQ4MmFjMjUwZWQiLCJpYXQiOjE3Njk3NzE1NDgsInN1YiI6IjhmMWJjMzQ1LWUxMmItNDI4MS1iYmU4LTVjMzY1MTQ4MjhkMyJ9.VR8vN8DFh6AwpiuINKw2xctwGzbO7t3fNUqL0qay35bSJoBGtPf9QaY_eAzm7B6LoG_ONjPpHmnfyq0Y9WxysQqGg1yKPIYvfgGr_o1avdoJC4XToYEIIGJHCLO9Iq_J-e9FSefeZKEclK4hIbcxVnP-MOdIWk1nBQtHuRvBlAPEmrN9xMAT9zGnFDLLP4VAW07Xeipmyz5MJvabsI74DAFZsCFk7bb2fzXUP62q2a2tvfcEKfbWRcPnfSpsXwjSH9cLRWwR8ZUZvZajxzY4MpFzlVb5_iJhYgng1254whqbbnU9eszdBE_ppkY16E30Qr6EtfEGKtELxKMrSrZj-Q; hide-cookie-banner=3147861829-COOKIE_PREFERENCES_ALREADY_SET"
},
"type": "n8n-nodes-base.redis",
"typeVersion": 1,
"position": [
-16,
304
],
"id": "1fb70333-91c6-4090-8320-b25f841f0188",
"name": "set-cookies",
"credentials": {
"redis": {
"id": "e99faTHtS0aY2Rm7",
"name": "Redis account"
}
}
},
{
"parameters": {
"url": "https://www.mercadolivre.com.br/afiliados/linkbuilder",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "cookie",
"value": "={{ $json.cookie }}"
}
]
},
"options": {
"response": {
"response": {
"fullResponse": true
}
}
}
},
"id": "6482a987-f10b-40cf-810d-cc806c002783",
"name": "get-setcookies",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.1,
"position": [
432,
-16
]
},
{
"parameters": {
"jsCode": "const oldCookieString = $('data').first().json.cookie || \"\";\nconst setCookieArray = $(\"get-setcookies\").first().json.headers['set-cookie'] || [];\n\nlet cookieMap = {};\n\nif (oldCookieString) {\n oldCookieString.split(';').forEach(c => {\n const [key, ...val] = c.trim().split('=');\n if (key) cookieMap[key] = val.join('=');\n });\n}\n\nsetCookieArray.forEach(c => {\n const cookiePart = c.split(';')[0];\n const [key, ...val] = cookiePart.trim().split('=');\n if (key) cookieMap[key] = val.join('=');\n});\n\nconst newCookieString = Object.keys(cookieMap)\n .map(key => `${key}=${cookieMap[key]}`)\n .join('; ');\n\nreturn { cookies_atualizados: newCookieString };"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
656,
-16
],
"id": "5972c79b-ef4b-4f69-8c1e-ac95bcdb0e58",
"name": "merge cookies"
},
{
"parameters": {
"operation": "set",
"key": "cookies-mercadolivre",
"value": "={{ $json.cookies_atualizados }}"
},
"type": "n8n-nodes-base.redis",
"typeVersion": 1,
"position": [
880,
-16
],
"id": "439700b2-4320-4437-bab4-de0642369e5c",
"name": "set-cookies1",
"credentials": {
"redis": {
"id": "e99faTHtS0aY2Rm7",
"name": "Redis account"
}
}
},
{
"parameters": {
"httpMethod": "POST",
"path": "247918c2-235e-4bbc-aaf6-6ad1ec50b438",
"responseMode": "responseNode",
"options": {}
},
"type": "n8n-nodes-base.webhook",
"typeVersion": 2.1,
"position": [
-240,
-16
],
"id": "8f3c0755-07e6-49f4-a240-1e8880cae3f9",
"name": "Webhook",
"webhookId": "247918c2-235e-4bbc-aaf6-6ad1ec50b438"
},
{
"parameters": {
"options": {}
},
"type": "n8n-nodes-base.respondToWebhook",
"typeVersion": 1.5,
"position": [
1328,
-112
],
"id": "91222a1c-f2c3-4951-b272-a9369c4cd02a",
"name": "Respond to Webhook"
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 3
},
"conditions": [
{
"id": "570007f6-01ff-4705-97a9-f72da9b21756",
"leftValue": "={{ $json.status }}",
"rightValue": 200,
"operator": {
"type": "number",
"operation": "notEquals"
}
}
],
"combinator": "and"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.3,
"position": [
1328,
80
],
"id": "10f1beb1-5787-4abb-a483-074be42b3652",
"name": "If"
},
{
"parameters": {
"subject": "Erro na execução",
"message": "Houve um erro na execução da geração do link de afiliados",
"options": {}
},
"type": "n8n-nodes-base.gmail",
"typeVersion": 2.2,
"position": [
1552,
80
],
"id": "640bc129-f506-4c0b-8502-930833834d6c",
"name": "Send a message",
"webhookId": "e52270de-7b79-4379-8fdb-cebd58d17201",
"credentials": {
"gmailOAuth2": {
"id": "K7KDV3loUZ0yCamo",
"name": "teste@gmail.com"
}
}
}
],
"pinData": {},
"connections": {
"When clicking ‘Execute workflow’": {
"main": [
[
{
"node": "set-cookies",
"type": "main",
"index": 0
}
]
]
},
"data": {
"main": [
[
{
"node": "get-setcookies",
"type": "main",
"index": 0
}
]
]
},
"get-cookies": {
"main": [
[
{
"node": "data",
"type": "main",
"index": 0
}
]
]
},
"set-cookies": {
"main": [
[]
]
},
"get-setcookies": {
"main": [
[
{
"node": "merge cookies",
"type": "main",
"index": 0
}
]
]
},
"merge cookies": {
"main": [
[
{
"node": "set-cookies1",
"type": "main",
"index": 0
}
]
]
},
"set-cookies1": {
"main": [
[
{
"node": "HTTP Request",
"type": "main",
"index": 0
}
]
]
},
"Webhook": {
"main": [
[
{
"node": "get-cookies",
"type": "main",
"index": 0
}
]
]
},
"HTTP Request": {
"main": [
[
{
"node": "Respond to Webhook",
"type": "main",
"index": 0
},
{
"node": "If",
"type": "main",
"index": 0
}
]
]
},
"If": {
"main": [
[
{
"node": "Send a message",
"type": "main",
"index": 0
}
]
]
}
},
"active": true,
"settings": {
"executionOrder": "v1",
"availableInMCP": false
},
"versionId": "d3ff9274-b08b-40a9-9eb3-dd2b49d1ec2a",
"meta": {
"templateCredsSetupCompleted": true,
"instanceId": "4e474214bcf60c1518d0b243ca48faad8775bf534b0cde8f194334c4a5c35e19"
},
"id": "rMvMQExO79IgkZiiwenWV",
"tags": []
}