{"record":{"id":"77a8d291f70151b0","repo":"n8n-io/n8n","slug":"0","errorCode":"0","errorMessage":"Credential with ID \"${credentialId}\" was not found.","messagePattern":"Credential with ID \"(.+?)\" was not found\\.","errorType":"exception","errorClass":"CredentialNotFoundError","httpStatus":400,"severity":"error","filePath":"packages/cli/src/credentials/credentials.service.ts","lineNumber":1218,"sourceCode":"\t\t\t\t\t`This credential is assigned to credential use \"${result.credentialUseIds.join(', ')}\" and cannot be deleted`,\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tawait this.credentialsRepository.remove(credential);\n\t}\n\n\tasync test(userId: User['id'], credentials: ICredentialsDecrypted) {\n\t\treturn await this.credentialsTester.testCredentials(userId, credentials.type, credentials);\n\t}\n\n\tasync testById(userId: User['id'], credentialId: string) {\n\t\tconst storedCredential = await this.credentialsFinderService.findCredentialById(credentialId);\n\n\t\t// Dynamic-credential flows only; admins test instance credentials via testWithCredentials\n\t\tif (!storedCredential || storedCredential.usageScope !== 'project') {\n\t\t\tthrow new CredentialNotFoundError(credentialId);\n\t\t}\n\n\t\tconst credentials = await this.prepareCredentialsForTest({ storedCredential });\n\t\treturn await this.test(userId, credentials);\n\t}\n\n\tasync testWithCredentials(user: User, credentials: ICredentialsDecrypted) {\n\t\tconst storedCredential = await this.credentialsFinderService.findCredentialForUser(\n\t\t\tcredentials.id,\n\t\t\tuser,\n\t\t\t['credential:read'],\n\t\t\t{ includeInstanceCredentials: true },\n\t\t);\n\n\t\tif (!storedCredential) {\n\t\t\tif (credentials.id === '' && hasGlobalScope(user, 'credential:manageInstance')) {\n\t\t\t\tthis.validateInstanceCredentialData(credentials.data ?? {});\n\t\t\t\treturn await this.test(user.id, credentials);","sourceCodeStart":1200,"sourceCodeEnd":1236,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/cli/src/credentials/credentials.service.ts#L1200-L1236","documentation":"Thrown by CredentialsService.testById() when no stored credential matches the given ID, OR when the credential exists but is not project-scoped (e.g. it is an instance/provider credential). The method is reserved for the dynamic-credential test flow; instance credentials must be tested through testWithCredentials. CredentialNotFoundError extends UserError, surfacing as a not-found (HTTP 404) response.","triggerScenarios":"POST /credentials/{id}/test where {id} does not exist; calling testById on an instance-scoped (provider connection) credential, which the dynamic flow does not support; testing a credential that was deleted between page load and click.","commonSituations":"Stale credential ID in a deep link or bookmark; frontend test button routed to the wrong endpoint for instance credentials; deleted credential still listed in a cached UI; off-by-one copy/paste of an ID.","solutions":["Confirm the credential ID exists via GET /credentials/{id} before testing.","For instance/provider credentials, use the testWithCredentials flow (testWithCredentials endpoint) instead of testById.","Refresh the credentials list in the UI so the test button targets a current ID.","If the credential was deleted, recreate it or pick a valid one from the current list."],"exampleFix":"// before\nawait credentialsService.testById(userId, maybeInstanceId); // throws\n\n// after\nconst stored = await credentialsFinderService.findCredentialById(id);\nif (!stored || stored.usageScope !== 'project') {\n  return credentialsService.testWithCredentials(user, { id, type, data });\n}\nawait credentialsService.testById(userId, id);","handlingStrategy":"validation","validationCode":"const stored = await credentialsFinderService.findCredentialById(credentialId);\nif (!stored || stored.usageScope !== 'project') {\n  // route to testWithCredentials instead\n}\nawait credentialsService.testById(userId, credentialId);","typeGuard":"function isProjectCredentialId(id: unknown): id is string {\n  return typeof id === 'string' && id.trim().length > 0;\n}","tryCatchPattern":"try {\n  await credentialsService.testById(userId, credentialId);\n} catch (e) {\n  if (e instanceof CredentialNotFoundError) {\n    // refresh credential list, fall back to testWithCredentials for instance creds\n  }\n  throw e;\n}","preventionTips":["Differentiate the UI test action by credential usageScope (project vs instance).","Re-fetch the credential before testing to avoid stale IDs.","Prefer the type-appropriate test endpoint over a generic one."],"tags":["credentials","testing","instance-credentials","validation","rest-api"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}