Learn how to use data pinning in n8n to save and access persistent workflow data like configs, credentials, and reference info across runs without external storage.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Data pinning in n8n allows you to save specific data from a workflow execution for future use, making it accessible across different workflow runs. This feature is particularly useful for storing reference data, configuration settings, or any information that needs to be persistently available without relying on external storage solutions.
Step 1: Understanding Data Pinning in n8n
Data pinning is a feature in n8n that allows you to save specific data from your workflow execution and make it available for future workflow runs. When you pin data, it gets stored in the n8n database and can be accessed using specific n8n expressions. This is particularly useful for:
Data pinning helps you avoid making unnecessary API calls to retrieve data that doesn't change often and provides a simple storage mechanism within n8n itself.
Step 2: Accessing the Data Pinning Feature
To access the data pinning feature in n8n:
The pin icon allows you to save specific data items or the entire output of a node for future use.
Step 3: Pinning Data from a Node Output
To pin data from a node's output:
The pinned data will now be stored in the n8n database and can be accessed in future workflow executions.
Step 4: Pinning JSON Data Example
Here's a practical example of pinning JSON data:
// In the Code node
return [
{
json: {
configSettings: {
apiUrl: 'https://api.example.com',
maxRetries: 3,
timeout: 5000,
features: ['logging', 'caching', 'compression']
}
}
}
];
This configuration data is now pinned and available for use in any workflow.
Step 5: Using Pinned Data in Workflows
To access pinned data in your workflows:
$pinData
expression followed by the name of your pinned dataFor example, to use the previously pinned "appConfig" data:
// In an Expression node or field
$pinData.appConfig.apiUrl
// Returns: https://api.example.com
$pinData.appConfig.features[0]
// Returns: logging
This allows you to access any part of your pinned data structure in expressions throughout your workflows.
Step 6: Creating a Workflow that Uses Pinned Data
Let's create a workflow that uses the pinned configuration data:
{{ $pinData.appConfig.apiUrl }}/users
{{ $pinData.appConfig.timeout }}
// In the Function node
const maxRetries = $pinData.appConfig.maxRetries;
const items = $input.all();
// Implement retry logic using the pinned maxRetries value
return items.map(item => {
item.json.maxRetries = maxRetries;
return item;
});
This workflow now uses the pinned configuration data for making HTTP requests and implementing retry logic.
Step 7: Managing Pinned Data
To manage your pinned data in n8n:
Regular management of pinned data is important to avoid cluttering your n8n instance with unnecessary data.
Step 8: Updating Pinned Data
To update existing pinned data:
Alternatively, you can use the n8n API to programmatically update pinned data.
Step 9: Advanced Use Case: Creating a Key-Value Store
You can use data pinning to create a simple key-value store in n8n:
// Get existing store or create a new one
const existingStore = $pinData.keyValueStore || {};
// Update or add new key-value pairs
const updatedStore = {
...existingStore,
lastRun: new Date().toISOString(),
counter: (existingStore.counter || 0) + 1,
userIds: [1, 2, 3, 4, 5]
};
return [
{
json: {
keyValueStore: updatedStore
}
}
];
This pattern allows you to implement a simple persistent storage mechanism within n8n.
Step 10: Using Pinned Data with Expressions in Various Nodes
Data pinning works with any node that supports expressions. Here are some examples:
{{ $pinData.thresholds.errorLevel > $input.item.errorCount }}
{
"apiEndpoint": "{{ $pinData.appConfig.apiUrl }}/{{ $input.item.json.endpoint }}",
"headers": {
"Authorization": "Bearer {{ $pinData.credentials.apiToken }}",
"Content-Type": "application/json"
}
}
// Process items based on pinned configuration
const config = $pinData.processingConfig;
const items = $input.all();
return items.map(item => {
if (config.enableProcessing) {
// Apply transformations based on config
item.json.processed = true;
if (config.applyNormalization) {
item.json.value = normalizeValue(item.json.value, config.normalizationFactor);
}
if (item.json.category in config.categoryMappings) {
item.json.mappedCategory = config.categoryMappings[item.json.category];
}
}
return item;
});
function normalizeValue(value, factor) {
return (value / factor).toFixed(2);
}
These examples demonstrate how pinned data can be used in different contexts within your workflows.
Step 11: Sharing Pinned Data Between Workflows
One powerful aspect of data pinning is the ability to share data between different workflows:
For example, create a "Configuration Manager" workflow that pins updated configuration data, then have other workflows reference this pinned data to always use the latest settings.
Step 12: Best Practices for Data Pinning
Follow these best practices when using data pinning in n8n:
Following these practices will help you maintain a clean and efficient data pinning system in n8n.
Step 13: Troubleshooting Data Pinning Issues
If you encounter issues with data pinning:
$pinData.yourPinnedDataName
For debugging, add a Function node with this code:
// Debug pinned data access
return [
{
json: {
availablePinnedData: Object.keys($pinData || {}),
myPinnedData: $pinData.yourPinnedDataName || 'Not found',
fullPinData: $pinData
}
}
];
This will help you see what pinned data is available and diagnose access issues.
Step 14: Data Pinning Limitations and Considerations
Be aware of these limitations when using data pinning:
For large datasets or highly sensitive information, consider using dedicated storage solutions like databases or secret management systems instead.
Step 15: Practical Example: Creating a Configuration Manager Workflow
Let's put it all together with a complete configuration management system using data pinning:
// Get existing configuration or initialize with defaults
const existingConfig = $pinData.systemConfig || {
version: 0,
lastUpdated: null,
environments: {}
};
// Create updated configuration
const newConfig = {
version: existingConfig.version + 1,
lastUpdated: new Date().toISOString(),
environments: {
development: {
apiUrl: 'https://dev-api.example.com',
timeout: 10000,
debugMode: true,
features: ['logging', 'detailed-errors']
},
production: {
apiUrl: 'https://api.example.com',
timeout: 5000,
debugMode: false,
features: ['logging', 'caching', 'rate-limiting']
},
...existingConfig.environments // Preserve any other existing environments
}
};
return [
{
json: {
systemConfig: newConfig
}
}
];
{{ $pinData.systemConfig.environments.production.apiUrl }}/data
{{ $pinData.systemConfig.environments.production.timeout }}
// Get configuration for current environment
const env = 'production'; // Could be dynamically determined
const config = $pinData.systemConfig.environments[env];
const items = $input.all();
// Apply environment-specific processing
return items.map(item => {
item.json.processedWith = {
configVersion: $pinData.systemConfig.version,
environment: env,
features: config.features,
debugMode: config.debugMode
};
// Apply debug logging if enabled
if (config.debugMode) {
console.log('Processing item in debug mode:', item.json);
}
return item;
});
This example demonstrates a complete configuration management system using data pinning, with a dedicated workflow for updating configurations and another workflow that uses those configurations.
Conclusion
Data pinning in n8n provides a powerful way to store and access persistent data across workflow executions. By following this guide, you've learned how to pin data, access it using expressions, update it, and implement advanced patterns like key-value stores and configuration management systems. Data pinning is particularly useful for storing configuration settings, credentials, reference data, and creating simple storage mechanisms within n8n itself.
Remember to follow the best practices outlined in this guide to maintain a clean and efficient data pinning system. For larger datasets or highly sensitive information, consider using dedicated storage solutions instead of relying solely on n8n's data pinning capability.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.