Start here: n8n OpenAI node: how to use?
This tutorial shows n8n OpenAI node: how to use in a Docker environment and gives a quick direct answer. To use the OpenAI node, add it to a workflow, set your OpenAI API key in credentials, and pass a prompt to the node to get generated text back.
What You Need
- Docker installed on your machine.
- An OpenAI API key (secret token).
- n8n Docker image (official).
- Basic comfort with workflows and JSON for mapping data.
How to install n8n with Docker
Run n8n with the OpenAI key set as an environment variable. The example below starts n8n on port 5678. Replace YOUR_OPENAI_KEY with your real key.
docker run -it --rm \
-p 5678:5678 \
-e OPENAI_API_KEY="YOUR_OPENAI_KEY" \
-e N8N_BASIC_AUTH_ACTIVE=true \
-e N8N_BASIC_AUTH_USER=n8nuser \
-e N8N_BASIC_AUTH_PASSWORD=changeme \
n8nio/n8n:latest
Alternative: use docker-compose for persistent data and safer secret management.
version: '3'
services:
n8n:
image: n8nio/n8n:latest
ports:
- '5678:5678'
environment:
- OPENAI_API_KEY=${OPENAI_API_KEY}
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER=n8nuser
- N8N_BASIC_AUTH_PASSWORD=changeme
volumes:
- ~/.n8n:/home/node/.n8n
Step 1: Start n8n and access the editor
Open http://localhost:5678 in your browser. Log in with the basic auth user and password you set. This gives you the n8n editor to build workflows and add nodes.
Step 2: Add the OpenAI node
In the editor add a new node and search for “OpenAI”. Create credentials using your API key. Choose a model like gpt-4 or gpt-3.5-turbo for text generation.
- Add a Set node to define input text. Set a field named prompt with a sample question.
- Connect the Set node to the OpenAI node.
- In the OpenAI node choose the action “Chat Completion” or “Text Completion” and map the prompt field.
// Example: Set node output (JSON)
{
"prompt": "Summarize the features of n8n in two short sentences."
}
// In OpenAI node, set 'Prompt' to an expression like:
{{$json["prompt"]}}
Step 3: Run and inspect results
Execute the workflow. The OpenAI node returns completion text in the node output. Use a Debug or Set node to save or forward the generated text. For example, forward results to Slack or email using built-in nodes.
Update
Keep n8n and the OpenAI node current. Pull the latest Docker image regularly. Example command to update the container image:
docker pull n8nio/n8n:latest
docker stop $(docker ps -q --filter ancestor=n8nio/n8n:latest)
docker run ... (your run command from above)
Security
- Do not hard-code your OpenAI key in public files.
- Use environment variables or a secrets manager for the API key.
- Enable n8n authentication and HTTPS in production.
- Limit model output and sanitize before sending to users or databases.
Done
You now have a working n8n workflow that uses the OpenAI API. You can extend prompts, add context, or chain nodes for automation. Common uses include summaries, code generation, and chat replies.