How to create n8n API KEY using Docker

Describe the issue/error/question

My application interacts with n8n api to create, update and execute n8n workflows.

I want to write some integration tests and for that I build a docker compose before running tests:

version: '3.8'

volumes:
  n8n_db_storage:
  redis_storage:

x-shared: &shared
  image: n8nio/n8n
  restart: always
  environment:
    - DB_TYPE=postgresdb
    - DB_POSTGRESDB_HOST=postgres
    - DB_POSTGRESDB_PORT=5432
    - DB_POSTGRESDB_DATABASE=n8n
    - DB_POSTGRESDB_USER=n8n
    - DB_POSTGRESDB_PASSWORD=45yJTe9Soc%t
    - EXECUTIONS_MODE=queue
    - QUEUE_BULL_REDIS_HOST=redis
    - QUEUE_HEALTH_CHECK_ACTIVE=true
    - N8N_CUSTOM_EXTENSIONS=/home/app/modules
  links:
    - postgres
    - redis
  volumes:
    - dist:/home/app/modules
    - node_modules:/home/app/modules/node_modules
  depends_on:
    redis:
      condition: service_healthy
    postgres:
      condition: service_healthy

services:
  postgres:
    image: postgres:11
    restart: always
    environment:
      - POSTGRES_USER=n8n
      - POSTGRES_PASSWORD=45yJTe9Soc%t
      - POSTGRES_DB=n8n
    volumes:
      - n8n_db_storage:/var/lib/postgresql/data
    ports:
      - 5432:5432
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -h localhost -U n8n -d n8n']
      interval: 5s
      timeout: 5s
      retries: 10

  redis:
    image: redis:6-alpine
    restart: always
    volumes:
      - redis_storage:/data
    ports:
      - 6379:6379
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 5s
      timeout: 5s
      retries: 10

  n8n:
    <<: *shared
    command: /bin/sh -c "n8n start"
    ports:
      - 5678:5678

  n8n-worker:
    <<: *shared
    command: /bin/sh -c "sleep 5; n8n worker"
    depends_on:
      - n8n

Now I want to create an integration test that creates workflow.

My question is how to create an API KEY as this test runs in CI server and every time its a fresh new n8n instance?

What is the error message (if any)?

N/A

Please share the workflow

N/A

Share the output returned by the last node

N/A

Information on your n8n setup

  • n8n version: 0.213.0
  • Database you’re using (default: SQLite): Postgres
  • Running n8n with the execution process [own(default), main]: main
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Docker

The solution that I found so far is to use: N8N_AUTH_EXCLUDE_ENDPOINTS=workflows.

Maybe in the future n8n could support create api_key as environment variable, from what I’ve seen it would be necessary to create the whole user as api_key belongs to user.

The above solution doesn’t work to create workflows.

The solution I came up is:

  • Start docker
  • Before run integration tests connect to n8n database and update n8n’s user table with specific apiKey
    • SQL: UPDATE "user" SET apiKey = '<YOUR_API_KEY>
  • Run integration tests
1 Like

Hi @ndeitch, welcome to the community!

That’s a sweet find, thanks so much for sharing :slight_smile:

1 Like

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