exportclassNasaPicsimplementsINodeType{description:INodeTypeDescription={// Basic node details will go hereproperties:[// Resources and operations will go here]};}
displayName:'NASA Pics',name:'NasaPics',icon:'file:nasapics.svg',group:['transform'],version:1,subtitle:'={{$parameter["operation"] + ": " + $parameter["resource"]}}',description:'Get data from NASAs API',defaults:{name:'NASA Pics',},inputs:['main'],outputs:['main'],credentials:[{name:'NasaPicsApi',required:true,},],requestDefaults:{baseURL:'https://api.nasa.gov',headers:{Accept:'application/json','Content-Type':'application/json',},},
n8n 使用 中设置的一些属性在编辑器 UI 中渲染节点。这些属性包括、 、和。
步骤 3.4:添加资源
资源对象定义了节点使用的 API 资源。在本教程中,您将创建一个节点来访问 NASA 的两个 API 端点:和。这意味着您需要在 中定义两个资源选项。使用资源对象更新数组:
1 2 3 4 5 6 7 8 9101112131415161718192021
properties:[{displayName:'Resource',name:'resource',type:'options',noDataExpression:true,options:[{name:'Astronomy Picture of the Day',value:'astronomyPictureOfTheDay',},{name:'Mars Rover Photos',value:'marsRoverPhotos',},],default:'astronomyPictureOfTheDay',},// Operations will go here]
{displayName:'Operation',name:'operation',type:'options',noDataExpression:true,displayOptions:{show:{resource:['astronomyPictureOfTheDay',],},},options:[{name:'Get',value:'get',action:'Get the APOD',description:'Get the Astronomy Picture of the day',routing:{request:{method:'GET',url:'/planetary/apod',},},},],default:'get',},{displayName:'Operation',name:'operation',type:'options',noDataExpression:true,displayOptions:{show:{resource:['marsRoverPhotos',],},},options:[{name:'Get',value:'get',action:'Get Mars Rover photos',description:'Get photos from the Mars Rover',routing:{request:{method:'GET',},},},],default:'get',},{displayName:'Rover name',description:'Choose which Mars Rover to get a photo from',required:true,name:'roverName',type:'options',options:[{name:'Curiosity',value:'curiosity'},{name:'Opportunity',value:'opportunity'},{name:'Perseverance',value:'perseverance'},{name:'Spirit',value:'spirit'},],routing:{request:{url:'=/mars-photos/api/v1/rovers/{{$value}}/photos',},},default:'curiosity',displayOptions:{show:{resource:['marsRoverPhotos',],},},},{displayName:'Date',description:'Earth date',required:true,name:'marsRoverDate',type:'dateTime',default:'',displayOptions:{show:{resource:['marsRoverPhotos',],},},routing:{request:{// You've already set up the URL. qs appends the value of the field as a query stringqs:{earth_date:'={{ new Date($value).toISOString().substr(0,10) }}',},},},},// Optional/additional fields will go here
这段代码创建了两个操作:一个用于获取今天的 APOD 图像,另一个用于发送获取火星探测器照片的请求。该对象要求用户选择要从哪个探测器获取照片。火星探测器操作中的对象引用了该对象来创建 API 调用的 URL。
{displayName:'Additional Fields',name:'additionalFields',type:'collection',default:{},placeholder:'Add Field',displayOptions:{show:{resource:['astronomyPictureOfTheDay',],operation:['get',],},},options:[{displayName:'Date',name:'apodDate',type:'dateTime',default:'',routing:{request:{// You've already set up the URL. qs appends the value of the field as a query stringqs:{date:'={{ new Date($value).toISOString().substr(0,10) }}',},},},},],}
import{IAuthenticateGeneric,ICredentialType,INodeProperties,}from'n8n-workflow';exportclassNasaPicsApiimplementsICredentialType{name='NasaPicsApi';displayName='NASA Pics API';// Uses the link to this tutorial as an example// Replace with your own docs links when building your own nodesdocumentationUrl='https://docs.n8n.io/integrations/creating-nodes/build/declarative-style-node/';properties:INodeProperties[]=[{displayName:'API Key',name:'apiKey',type:'string',default:'',},];authenticate={type:'generic',properties:{qs:{'api_key':'={{$credentials.apiKey}}'}},}asIAuthenticateGeneric;}
{// All node names must start with "n8n-nodes-""name":"n8n-nodes-nasapics","version":"0.1.0","description":"n8n node to call NASA's APOD and Mars Rover Photo services.","keywords":[// This keyword is required for community nodes"n8n-community-node-package"],"license":"MIT","homepage":"https://n8n.io","author":{"name":"Test","email":"test@example.com"},"repository":{"type":"git",// Change the git remote to your own repository// Add the new URL here"url":"git+<your-repo-url>"},"main":"index.js","scripts":{// don't change},"files":["dist"],// Link the credentials and node"n8n":{"n8nNodesApiVersion":1,"credentials":["dist/credentials/NasaPicsApi.credentials.js"],"nodes":["dist/nodes/NasaPics/NasaPics.node.js"]},"devDependencies":{// don't change},"peerDependencies":{// don't change}}