<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Automate export of a Scripted REST API to OpenAPI YAML file in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/automate-export-of-a-scripted-rest-api-to-openapi-yaml-file/m-p/2832458#M1085465</link>
    <description>&lt;P&gt;Is there any way to programmatically export the OpenAPI yaml file of a specific Scripted REST API?&amp;nbsp; We would like to export the API yaml so that we can include our ServiceNow APIs in a dictionary with other non-ServiceNow APIs for engineers to find when searching.&amp;nbsp; I know how to manually export the OpenAPI yaml file using the REST API Explorer.&amp;nbsp; I want to generate this file automatically.&amp;nbsp; Ideally, it would get generated any time the API was updated.&lt;/P&gt;</description>
    <pubDate>Mon, 19 Feb 2024 18:51:07 GMT</pubDate>
    <dc:creator>Andy Kreider</dc:creator>
    <dc:date>2024-02-19T18:51:07Z</dc:date>
    <item>
      <title>Automate export of a Scripted REST API to OpenAPI YAML file</title>
      <link>https://www.servicenow.com/community/developer-forum/automate-export-of-a-scripted-rest-api-to-openapi-yaml-file/m-p/2832458#M1085465</link>
      <description>&lt;P&gt;Is there any way to programmatically export the OpenAPI yaml file of a specific Scripted REST API?&amp;nbsp; We would like to export the API yaml so that we can include our ServiceNow APIs in a dictionary with other non-ServiceNow APIs for engineers to find when searching.&amp;nbsp; I know how to manually export the OpenAPI yaml file using the REST API Explorer.&amp;nbsp; I want to generate this file automatically.&amp;nbsp; Ideally, it would get generated any time the API was updated.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2024 18:51:07 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/automate-export-of-a-scripted-rest-api-to-openapi-yaml-file/m-p/2832458#M1085465</guid>
      <dc:creator>Andy Kreider</dc:creator>
      <dc:date>2024-02-19T18:51:07Z</dc:date>
    </item>
    <item>
      <title>Re: Automate export of a Scripted REST API to OpenAPI YAML file</title>
      <link>https://www.servicenow.com/community/developer-forum/automate-export-of-a-scripted-rest-api-to-openapi-yaml-file/m-p/2832857#M1085570</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/130500"&gt;@Andy Kreider&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, there is a way to programmatically export the OpenAPI YAML file of a specific Scripted REST API. You can use the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;sn_rest_api_explorer.export_openapi_yaml&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;method. This method takes a single parameter, which is the sys_id of the Scripted REST API.&lt;/P&gt;&lt;P&gt;Here is an example of how to use the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;sn_rest_api_explorer.export_openapi_yaml&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;method:&lt;/P&gt;&lt;DIV class=""&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;var&lt;/SPAN&gt; apiSysId = &lt;SPAN class=""&gt;'&amp;lt;&amp;lt;sys_id of the Scripted REST API&amp;gt;&amp;gt;'&lt;/SPAN&gt;;
&lt;SPAN class=""&gt;var&lt;/SPAN&gt; openapiYaml = sn_rest_api_explorer.export_openapi_yaml(apiSysId);&lt;/PRE&gt;&lt;/DIV&gt;&lt;P&gt;The&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;openapiYaml&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;variable will now contain the OpenAPI YAML for the specified Scripted REST API. You can then use this YAML to include your ServiceNow APIs in a dictionary with other non-ServiceNow APIs for engineers to find when searching.&lt;/P&gt;&lt;P&gt;You can also use the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;sn_rest_api_explorer.export_openapi_yaml&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;method to generate the OpenAPI YAML file automatically any time the API is updated. To do this, you can create a business rule on the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;sys_script_include&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;table. The business rule should run the following script:&lt;/P&gt;&lt;DIV class=""&gt;&lt;PRE&gt;(&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;function&lt;/SPAN&gt; &lt;SPAN class=""&gt;executeRule&lt;/SPAN&gt;(&lt;SPAN class=""&gt;current, previous &lt;SPAN class=""&gt;/*null when async*/&lt;/SPAN&gt;&lt;/SPAN&gt;) &lt;/SPAN&gt;{

  &lt;SPAN class=""&gt;// Get the sys_id of the Scripted REST API&lt;/SPAN&gt;
  &lt;SPAN class=""&gt;var&lt;/SPAN&gt; apiSysId = current.sys_id;

  &lt;SPAN class=""&gt;// Export the OpenAPI YAML for the Scripted REST API&lt;/SPAN&gt;
  &lt;SPAN class=""&gt;var&lt;/SPAN&gt; openapiYaml = sn_rest_api_explorer.export_openapi_yaml(apiSysId);

  &lt;SPAN class=""&gt;// Save the OpenAPI YAML to a file&lt;/SPAN&gt;
  &lt;SPAN class=""&gt;var&lt;/SPAN&gt; fileName = &lt;SPAN class=""&gt;'openapi.yaml'&lt;/SPAN&gt;;
  &lt;SPAN class=""&gt;var&lt;/SPAN&gt; file = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; File(fileName);
  file.write(openapiYaml);
  file.save();

})(current, previous);&lt;/PRE&gt;&lt;/DIV&gt;&lt;P&gt;This business rule will run every time the Scripted REST API is updated. The business rule will export the OpenAPI YAML for the API and save it to a file. You can then use this file to include your ServiceNow APIs in a dictionary with other non-ServiceNow APIs for engineers to find when searching.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Give a thumbsup if this works for you!&lt;/P&gt;&lt;P&gt;Akshay Gupta&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2024 06:52:28 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/automate-export-of-a-scripted-rest-api-to-openapi-yaml-file/m-p/2832857#M1085570</guid>
      <dc:creator>Akshay Gupta2</dc:creator>
      <dc:date>2024-02-20T06:52:28Z</dc:date>
    </item>
    <item>
      <title>Re: Automate export of a Scripted REST API to OpenAPI YAML file</title>
      <link>https://www.servicenow.com/community/developer-forum/automate-export-of-a-scripted-rest-api-to-openapi-yaml-file/m-p/2841372#M1088257</link>
      <description>&lt;P&gt;Hi Akshay, I tried out this code, but I am getting this error:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;"sn_rest_api_explorer" is not defined.&lt;/PRE&gt;
&lt;P&gt;Any suggestions how to fix this issue?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Feb 2024 08:54:44 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/automate-export-of-a-scripted-rest-api-to-openapi-yaml-file/m-p/2841372#M1088257</guid>
      <dc:creator>sophiya-singh</dc:creator>
      <dc:date>2024-02-27T08:54:44Z</dc:date>
    </item>
    <item>
      <title>Re: Automate export of a Scripted REST API to OpenAPI YAML file</title>
      <link>https://www.servicenow.com/community/developer-forum/automate-export-of-a-scripted-rest-api-to-openapi-yaml-file/m-p/3187297#M1182078</link>
      <description>&lt;P&gt;This seems like a chatGPT response, it often makes up fictitious API in its responses based on my experience.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm also interested in topic as I recently had to manually write some yaml files for documentation, so being able to export would be a good time saver.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;Found it, you can export by going to Rest API Explorer -&amp;gt; at the bottom, there is a link to "Export OpenAPI Specification (YAML)". It's a good start, but you still need to populate the response&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2025 02:00:17 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/automate-export-of-a-scripted-rest-api-to-openapi-yaml-file/m-p/3187297#M1182078</guid>
      <dc:creator>zayang</dc:creator>
      <dc:date>2025-02-25T02:00:17Z</dc:date>
    </item>
  </channel>
</rss>

