DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • The Noticeable Shift in SIEM Data Sources
  • Microservices Decoded: Unraveling the Benefits, Challenges, and Best Practices for APIs
  • Low Code Approach for Building a Serverless REST API
  • How to Configure AWS Glue Job Using Python-Based AWS CDK

Trending

  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • Medallion Architecture: Why You Need It and How To Implement It With ClickHouse
  • Docker Model Runner: Streamlining AI Deployment for Developers
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Invoke an External REST API from a Cloud Function

How to Invoke an External REST API from a Cloud Function

From creating your first cloud function, we now show you how you can create a function that invokes a REST API from a third party.

By 
Max Katz user avatar
Max Katz
·
Jul. 24, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
18.2K Views

Join the DZone community and get the full member experience.

Join For Free

In a previous blog post, I showed how to create your first cloud function (plus a video). It's very likely that your cloud function will need to invoke an external REST API. The following tutorial will show you how to create such a function (it's very easy).

  1. Sign into an IBM Cloud account
  2. Click Catalog
  3. Remove the label:lite filter and type
  4. Click on Functions box
  5. Click the Start Creating button
  6. Click Create Action
  7. For Action Name, enter "ajoke" and click the Create button. A new cloud function will be created with Hello World message
  8. Replace the function code with the following code which invokes a 3rd party REST API which returns a random joke:
    
    var request = require("request");
    
    function main(params) {
       var options = {
          url: "https://api.icndb.com/jokes/random",
          json: true
       };
    
       return new Promise(function (resolve, reject) {
          request(options, function (err, resp) {
             if (err) {
                console.log(err);
                return reject({err: err});
             }
          return resolve({joke:resp.body.value.joke});
          });
       });
    }
    
    • The code is simple. It uses the request Node.js package to connect to an external REST API
    • The external REST API returns a random joke
    • A JavaScript Promise is used for invoking the REST API
    • At the end, the cloud function returns a response in JSON format
  9. Now click the Save button to save the code. Once the code is saved, the button will change to Invoke. Click the button to invoke the function. In the right-hand panel you should see output with a random joke:
    {
      "joke": "Project managers never ask Chuck Norris for estimations... ever."
    }

This is how it looks inside the IBM Cloud Functions editor:

Image title


Of course, you can also build and test a cloud function using the CLI. I'll cover that in another blog post.

For now, let's expose this cloud function as a REST API so we can invoke it outside the console. In fact, you will be able to invoke it directly from the browser once we make it a Web Action.

  1. On the left-hand side, click Endpoints
  2. Check Enable as Web Action and click Save
  3. Copy the URL and enter into a browser's address bar

Here is how it looks in Firefox:

Image title


That was easy, right?

In this blog post, you learned how to create a cloud function which invokes an external (3rd party) API. It's very likely that even the simplest application will need to get data from an external API so this a good example/template to have.

API REST Web Protocols Cloud

Published at DZone with permission of Max Katz, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Noticeable Shift in SIEM Data Sources
  • Microservices Decoded: Unraveling the Benefits, Challenges, and Best Practices for APIs
  • Low Code Approach for Building a Serverless REST API
  • How to Configure AWS Glue Job Using Python-Based AWS CDK

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: