Skip to content

feat (public-api): update credentials - #18082

Open
Shock3udt wants to merge 1 commit into
n8n-io:masterfrom
Shock3udt:master
Open

feat (public-api): update credentials#18082
Shock3udt wants to merge 1 commit into
n8n-io:masterfrom
Shock3udt:master

Conversation

@Shock3udt

Copy link
Copy Markdown

Summary

This PR adds a new PUT /api/v1/credentials/{id} endpoint to the n8n public API, allowing users to update existing credentials programmatically.

🚀 Features Added:

  • Update Credential Endpoint: PUT /api/v1/credentials/{id} with support for updating both name and data properties
  • API Key Scope: New credential:update scope added to API key permissions system
  • Validation: Proper request validation with optional data encryption
  • Authorization: Permission checks ensuring only credential owners/editors or global admins can update
  • Error Handling: Comprehensive error responses (404 for not found, 403 for insufficient permissions)

🔧 Implementation Details:

  • Added updateCredential service function with encryption support
  • Created validCredentialUpdate middleware for request validation
  • Added OpenAPI specification with complete documentation
  • Integrated with existing event system for audit trails
  • Added unit tests for error scenarios

📋 API Usage:

curl -X PUT "https://your-n8n-instance.com/api/v1/credentials/{id}" \
  -H "X-N8N-API-KEY: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Updated Credential Name",
    "data": {
      "token": "new-encrypted-token-value"
    }
  }'

Testing:

  • Manual testing with API requests
  • Unit tests for service functions
  • Error scenario coverage
  • Build verification completed successfully

Related Linear tickets, Github issues, and Community forum posts

Review / Merge checklist

  • PR title and summary are descriptive. (conventions)
  • Docs updated or follow-up ticket created.
  • Tests included.
  • PR Labeled with release/backport (if the PR is an urgent fix that needs to be backported)

📁 Files Modified:

Core Implementation:

  • packages/cli/src/public-api/v1/handlers/credentials/credentials.service.ts - Added updateCredential function
  • packages/cli/src/public-api/v1/handlers/credentials/credentials.handler.ts - Added update endpoint handler
  • packages/cli/src/public-api/v1/handlers/credentials/credentials.middleware.ts - Added validation middleware
  • packages/cli/src/public-api/types.ts - Added Update request type

Permissions & Scopes:

  • packages/@n8n/permissions/src/constants.ee.ts - Added credential:update scope
  • packages/@n8n/permissions/src/public-api-permissions.ee.ts - Added scope to role permissions

API Documentation:

  • packages/cli/src/public-api/v1/handlers/credentials/spec/paths/credentials.id.yml - Added PUT operation
  • packages/cli/src/public-api/v1/handlers/credentials/spec/schemas/update-credential.yml - New schema file

Tests:

  • packages/cli/src/public-api/v1/handlers/credentials/__tests__/credentials.service.test.ts - Added unit tests

🎯 Breaking Changes:

None - this is a purely additive feature that maintains backward compatibility.

🔒 Security Considerations:

  • Proper authorization checks implemented
  • API key scope validation
  • Credential data encryption maintained
  • Permission-based access control enforced

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cubic analysis

3 issues found across 9 files • Review in cubic

React with 👍 or 👎 to teach cubic. You can also tag @cubic-dev-ai to give feedback, ask questions, or re-run the review.

return next();
};

export const validCredentialUpdate = (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Middleware is named “validCredentialUpdate” but never validates the input. Regardless of whether data is present, the request proceeds unverified, so malformed or malicious credential data can reach the service layer unchecked. Implement proper schema/type validation similar to validCredentialsProperties or remove this no-op middleware.

Prompt for AI agents
Address the following comment on packages/cli/src/public-api/v1/handlers/credentials/credentials.middleware.ts at line 51:

<comment>Middleware is named “validCredentialUpdate” but never validates the input. Regardless of whether `data` is present, the request proceeds unverified, so malformed or malicious credential data can reach the service layer unchecked. Implement proper schema/type validation similar to `validCredentialsProperties` or remove this no-op middleware.</comment>

<file context>
@@ -47,3 +47,20 @@ export const validCredentialsProperties = (
 
 	return next();
 };
+
+export const validCredentialUpdate = (
+	req: CredentialRequest.Update,
+	res: express.Response,
</file context>


it('should throw error when credential not found', async () => {
// Mock getCredentials to return null
jest.doMock('../credentials.service', () => ({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest.doMock is called after the module has already been imported, so the intended mock for getCredentials/getSharedCredentials will not be applied and the test assertions may be invalid. (Based on your team's feedback about ensuring tests are reliable and mocks are applied correctly.)

Prompt for AI agents
Address the following comment on packages/cli/src/public-api/v1/handlers/credentials/__tests__/credentials.service.test.ts at line 88:

<comment>jest.doMock is called after the module has already been imported, so the intended mock for getCredentials/getSharedCredentials will not be applied and the test assertions may be invalid. (Based on your team&#39;s feedback about ensuring tests are reliable and mocks are applied correctly.)</comment>

<file context>
@@ -68,4 +69,44 @@ describe(&#39;CredentialsService&#39;, () =&gt; {
 			).toBe(true);
 		});
 	});
+
+	describe(&#39;updateCredential&#39;, () =&gt; {
+		const mockUser: User = {
+			id: &#39;user1&#39;,
+			role: &#39;global:member&#39;,
+		} as User;
</file context>

tempCredential.name = existingCredential.name;
tempCredential.type = existingCredential.type;
// Temporarily assign the decrypted data (will be encrypted by encryptCredential)
(tempCredential as any).data = properties.data;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rule violated: Prefer Typeguards over Type casting

Avoid as any type assertion; it bypasses type safety and violates the "Prefer Typeguards over Type casting" rule. Declare data on CredentialsEntity (or use a properly typed helper) instead of casting.

Prompt for AI agents
Address the following comment on packages/cli/src/public-api/v1/handlers/credentials/credentials.service.ts at line 132:

<comment>Avoid `as any` type assertion; it bypasses type safety and violates the &quot;Prefer Typeguards over Type casting&quot; rule. Declare `data` on `CredentialsEntity` (or use a properly typed helper) instead of casting.</comment>

<file context>
@@ -96,6 +96,65 @@ export async function saveCredential(
 	return result;
 }
 
+export async function updateCredential(
+	user: User,
+	credentialId: string,
+	properties: Partial&lt;CredentialRequest.CredentialProperties&gt;,
+): Promise&lt;CredentialsEntity&gt; {
+	// Get existing credential
</file context>

@n8n-assistant n8n-assistant Bot added community Authored by a community member core Enhancement outside /nodes-base and /editor-ui in linear DEPRECATED labels Aug 7, 2025
@Joffcom

Joffcom commented Aug 7, 2025

Copy link
Copy Markdown
Member

Hey @Shock3udt,

Thank you for your contribution. We appreciate the time and effort you’ve taken to submit this pull request.

Before we can proceed, please ensure the following:
• Tests are included for any new functionality, logic changes or bug fixes.
• The PR aligns with our contribution guidelines.

Regarding new nodes:
We no longer accept new nodes directly into the core codebase. Instead, we encourage contributors to follow our Community Node Submission Guide to publish nodes independently.

If your node integrates with an AI service that you own or represent, please email nodes@n8n.io and we will be happy to discuss the best approach.

About review timelines:
This PR has been added to our internal tracker as "GHC-3571". While we plan to review it, we are currently unable to provide an exact timeframe. Our goal is to begin reviews within a month, but this may change depending on team priorities. We will reach out when the review begins.

Thank you again for contributing to n8n.

@ericmyrem

Copy link
Copy Markdown

Please incorporate this PR - not being able to update credentials and credential updates not getting pushed through the git feature is a major headache for enterprises @Joffcom @janober

@Joffcom

Joffcom commented Aug 15, 2025

Copy link
Copy Markdown
Member

Hey @ericmyrem

Please don't tag people to try and get something reviewed sooner, This is somewhere in the list to be reviewed by the team that look after the api which isn't myself or Jan.

There are other PRs that touch the api which may be reviewed sooner so this may also end up being closed if another pr adds the same feature.

This is also not related to the git feature looking at the description and we wouldn't recommend storing credentials in git because of the potential security risks.

@ericmyrem

Copy link
Copy Markdown

Sure thing, no more tagging - it’s probably worth some attention though since the git functionality should include updates to credentials.

Enterprises use it with the external secrets feature which means credentials don’t get stored in git but the {{secrets. }} expressions do.

Currently the secrets references break when they get updated in dev, the git feature doesn’t include the update, software gets released to test and entire workflows break due to the wrong / outdated secrets references.

So having this update API or git supporting secret updates is essential for your enterprise customers.

@adamscherer-epsilon

Copy link
Copy Markdown

This is a critical item for our team as well. We intended on rotating certain credentials with an external process and that is not possible without this feature.

@henkdeblauw

Copy link
Copy Markdown

This is hugely important for credential rotation. We manage credentials for our customers through our own front-end and this is the missing piece.

@danone-dev

Copy link
Copy Markdown

This PR or something with similar functionality is essential to make credential rotation feasible in n8n. I cannot use long lived credentials an really need a way to rotate them easily.

@micahlucero

Copy link
Copy Markdown

+1 This would be a great addition

@longbkit

Copy link
Copy Markdown

+1
This is very important for standard Authentication flows, where access token has short expired time, and with refresh token to acquire new access token.

@merryech0

merryech0 commented Dec 3, 2025

Copy link
Copy Markdown

+1
this would be useful feature

@17jmumford

Copy link
Copy Markdown

+1 on this, need the ability to programmatically:

  • Update credentials
  • Share credentials
  • Remove access to credentials

@lviobio

lviobio commented Mar 27, 2026

Copy link
Copy Markdown

+1, Needed for credentials rotation

@sandra0503

Copy link
Copy Markdown
Contributor

Hello @Shock3udt, meanwhile we added a PATCH endpoint for changing credentials so the PUT is not needed right now - thanks a lot for opening a PR though!

@cla-bot

cla-bot Bot commented May 11, 2026

Copy link
Copy Markdown

Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. After signing, please comment @cla-bot check to re-check signature status.

@cla-bot

cla-bot Bot commented May 11, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@n8n-assistant n8n-assistant Bot added the Needs Feedback Waiting for further input or clarification. label Aug 3, 2026
@n8n-assistant

n8n-assistant Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Hi @Shock3udt 👋

Some of the checks on this PR are currently failing, which is blocking review.

Please take a look at the checks and fix them. If the failures don't look related to your changes, rebasing onto the latest master often clears them up.

If you're stuck on any of them, leave a comment here and we'll help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community Authored by a community member core Enhancement outside /nodes-base and /editor-ui in linear DEPRECATED Needs Feedback Waiting for further input or clarification.

Projects

None yet

Development

Successfully merging this pull request may close these issues.