Salesforce REST API lets you integrate Salesforce into the External system. With REST API

we can expose our salesforce org data to an external system or we can fetch data from an external system into the Salesforce org.

Now if we have created any API in our system then we have to test it if it is working fine for users. Several methods exist for testing APIs, but Swagger is a popular one.

Swagger is one of the tools for documenting API. You can test your API right in the documentation using Swagger. JSON or YAML can be used to define Swagger definitions.

So here in this blog, we tried to let you know how to configure your org so that you can test your APIs using swagger. Let’s begin

Complete guide to test APIs using Swagger

Here we are using a “httpget” request to make you familiar with how we can use the swagger API tool for exposing salesforce org data to an external system.

In this scenario, we hit a get request in the swagger editor and it will give us the order list in the form of JSON.

Here are the steps:

Step 1. First, make your apex class available for the Rest web services by using a global specifier for both class and method.

Code of APEX class

Step 2. Now in Setup go to the quick find box and type sites

Complete guide to test APIs using Swagger

Step 3. If you are not having any sites please create one and if you are having sites please select any site. Here we will permit the external user to access our class.

Complete guide to test APIs using Swagger1

Step 4. After selecting the sites click on “Public Access Settings”.

Complete guide to test APIs using Swagger2

   Step 5. Here click on “Enabled Apex Class Access”

Complete guide to test APIs using Swagger3

Step 6. Here we are going to add the apex class so click on edit.

Complete guide to test APIs using Swagger4

Step 7. Select the apex class which you have created as an API and add it

Complete guide to test APIs using Swagger5

Step 8. Now after adding the class to the site user we still have to add the swagger site to CORS so that the swagger website is able to access our org. So go to the quick find box and type CORS

Complete guide to test APIs using Swagger6

Step 9. Click on the New button

Complete guide to test APIs using Swagger7

Step 10. Add this Url to CORS

Complete guide to test APIs using Swagger8

Step 11.Create a Swagger file

Here we are done with our setting now we have to create a Swagger file for this go to this link and create a new file.

(i) Here first we have swagger as a starting element and its version must be not more than 2.0.
(ii) now in info define any version and title of your choice.
(iii) In the host tag define your org URL while in the path tag defines your base path and add /service/apexrest as it is necessary to hit APEX APIs.
(iv) after that in path define the URL/path that you have mentioned in your class. And start coding

As per the need of your class.

Complete guide to test APIs using Swagger9

Swagger Code:

Step 12. Once your swagger code is complete, on the right side you will get the UI to work on.

Here just for testing purposes paste our code and you will get a list of orders from our org. Click on GET Button and you will get the result.

Complete guide to test APIs using Swagger10

Step 13. Click on Execute Button and it will hit the API whose URL is provided in the swagger method.

Complete guide to test APIs using Swagger11

Step 14. Here you will get the response depending on the response body you have sent by your apex class.

Complete guide to test APIs using Swagger12

Conclusion

So, here it is, a complete guide on using Swagger to test APIs in salesforce.

We’d love to hear about your experience with the solution. Keep an eye out for more interesting Salesforce development solutions on our blogs and Stay tuned for the next one. Happy Learning!

Related Articles
Expert Guide On Nominal XIRR Calculation Using LWC in Salesforce

When building financial tools within Salesforce, handling irregular cash flows with precision is a must. Whether you’re developing investment trackers, portfolio performance dashboards, or funding monitors, calculating Nominal XIRR (Extended Internal Rate of Return) becomes essential. In this blog, you’ll learn how to build a Lightning Web Component (LWC) in Salesforce that calculates Nominal XIRR […]

Read More
Beginners Guide to Making Your LWC Component Multilingual

If you’re wondering how to make your LWC component multilingual, you’re already ahead of the curve. Global users expect content in their native language, and Salesforce gives you the tools to deliver exactly that. With Custom Labels and the Translation Workbench, you can easily localize your Lightning Web Components without touching a single line of […]

Read More
Hands-On Guide Building a Drag-and-Drop Form Builder Using Lightning Web Components

Drag-and-drop interfaces are a user experience staple, and when paired with the power of Lightning Web Components (LWC), they unlock dynamic, highly interactive Salesforce components. In this blog post, we’ll walk you through how to build a custom drag-and-drop form builder in LWC. Users can drag form fields like text inputs and dropdowns into a […]

Read More
Please Make Blog Banner How to Create an Agentforce Enabled Scratch Org in Salesforce 100 1

Setting up an Agentforce-enabled scratch org is the most important step for developers who want to experiment with AI features such as Prompt Builder and Einstein GPT. Whether you’re developing the next generation CRM software or experimenting with how generative AI may improve user experience, a scratch org equipped with Agentforce is your playground. Follow […]

Read More
Step-by-Step Guide to Enhance Your LWC Development Workflow with the Logger API

Are you looking to streamline your LWC development workflow but confused about how to do it? Then you are in the right place. The Salesforce Logger API is a powerful tool that helps you monitor, debug, and improve code quality in real time.  This step-by-step guide walks you through how to set it up and […]

Read More
Enhancing Your Salesforce Experience with a Custom Activity Component

In today’s fast-paced business landscape, productivity hinges on streamlined workflows and intuitive interfaces. While Salesforce offers robust activity tracking capabilities, sometimes the standard components fall short of specific organizational needs.  That’s where a Custom Activity Component comes in—designed to tailor your activity management experience, boost user efficiency, and provide deeper insights into customer interactions. In […]

Read More
Our Location worldwide
Indian Flag India
3rd Floor, A-10, Pegasus Tower, Sector 68, Noida, Uttar Pradesh 201301 +91-1203117884
United States of America Flag USA
333 West Brown Deer Road Unit G – 366 Milwaukee WI, USA 53217 +1(262) 310-7818
United Kingdom Flag UK
7 Bell Yard, London, WC2A 2JR +44 20 3239 9428
Canada Canada
HIC Global Solutions INC
43 Lafferty Lane, Richmond Hill, L4C 3N8, CA +1(262) 310-7818