Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • Go to Haptik Website
  • Contact Us
  • Home
  • Developer Guides
  • Code Step Integration

How to Send Entity Values to API to Fetch Data

Written by Medha Anand

Updated on April 19th, 2023

Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • Getting Started
    Build Deploy Analyse Manage Account Bot Deactivation
  • Bot Building
    Essentials Smart Skills Steps User Messages Bot Responses Entities Connections Customisations User feedback collection Testing Whatsapp Bots NLU Bot Maintenance
  • Smart Agent Chat
    Set up Admin Settings MyChats Section (Agent Inbox) Live Traffic Section Teams Section Archives Section Analytics Plans on Smart Agent Chat
  • Conversation Design
    Design Basics Design Guides Designing for Platforms Designing WhatsApp Bots
  • Developer Guides
    Code Step Integration Static Step Integration Shopify Integration SETU Integration Exotel Integration CIBIL integration Freshdesk KMS Integration PayU Integration Zendesk Guide Integration Twilio Integration Razorpay Integration LeadSquared Integration USU(Unymira) Integration Helo(VivaConnect) Integration Salesforce KMS Integration Stripe Integration PayPal Integration CleverTap Integration Fynd Integration HubSpot Integration Magento Integration WooCommerce Integration Microsoft Dynamics 365 Integration
  • Deployment
    Web SDK WhatsApp Facebook Instagram Sunshine Conversation LINE Google Business Messages Telegram MS Teams Bot as an API iOS SDK Android SDK
  • External Agent Tool Setup
    Zendesk Chat Salesforce Service Cloud Freshchat Zoho NICE CXOne Gorgias
  • Analytics & Reporting
    Intelligent Analytics
  • Notifications
    SMS Notifications Success Measurement
  • Commerce Plus
    Catalog Integration Bot Building Guide Channel Deployments Unified ML Pipeline Documentation
  • Troubleshooting Guides
    Error Messages FAQs
  • Release Notes
+ More

Sending entity values to API to fetch data

Once the entity value is stored in a variable on the Code step, you can use this variable to send this to the API and fetch the real-time data. You can send this variable as a payload to the API endpoint.

The following is a sample code for fetching order details -

def main(event, context):
 """
 event['body'] is a string dict with the following keys:
 node, event, user, entities.
 Currently, we pass user_id, user_name, full_name, device_platform and language_code in the user dictionary.
 Args:
 event (dict): Data corresponding to this event
 context
 Returns
 (dict): response with statusCode and the response for the User
 """
 body = json.loads(event['body'])
 entities = body.get('entities')
 user_data = body.get('user')
 conversation_details = body.get('conversation_details')
 env_variables = body.get("env_variables")
 entity = Entities(entities)
 order_id = entity.get('capture_track_orderid_copy', '')
 order_details = get_order_details(env_variables, order_id)
 if 'Statuscode' in order_details:
 return order_details
 message = get_order_message(order_details)
 # get_order_message() returns order details in a button HSL 
 final_response = {
 'status': True,
 'message': message
 }
 response = {'statusCode': 200, 'body': json.dumps(final_response), 'headers': {'Content-Type': 'application/json'}}
 return response
def get_order_details(env_variables, order_id):
 url = "<API_ENDPOINT>"
 payload = {
 "order_id": order_id.upper()
 }
 headers = {
 'Content-Type': 'application/json'
 }
 try:
 response = requests.request(
 "POST",
 url,
 headers=headers,
 data = json.dumps(payload),
 timeout=7
 )
 except requests.exceptions.Timeout as errt:
 print("Timeout Error:", errt)
 error = "Timeout Error"
 response = generate_response(error)
 return response
 except Exception as err:
 print("Oops: Something Else", err)
 error = "Api Failure"
 response = generate_response(error)
 return response
 if response.status_code == 200:
 order_details = json.loads(response.text)
 order_details = order_details.get('results', {})
 return order_details
 else:
 print("API FAILED. STATUS CODE: ", response.status_code, response.text)
 error = "Api Failure"
 response = generate_response(error)
 return response

You cannot set the entity value from the Code Editor.

Was this article helpful?

Yes
No
Give feedback about this article

Related Articles

  • What is Code Step? How to Use Code Step?
  • How to Use Entity Values on Code Step
  • How to Create HSLs in Code Editor
  • Using Logger Support for Debugging

Platform

  • Conversation Studio
  • Smart Skills
  • Advanced NLU
  • Intelligent Analytics
  • Omnichannel
  • Smart Agent Chat
  • Enterprise Security
  • Integrations

Solutions

  • Conversational Commerce
  • Lead Generation
  • Customer Care
  • WhatsApp
  • Conversational IVR
  • Google Business Messages

Industries

  • Retail/ E-Commerce
  • Financial Services
  • Travel & Hospitality
  • Telecom

Knowledge

  • ROI Calculator
  • Reports & Research
  • Case Studies
  • Webinars
  • ISAT
  • Tech Blog
  • Business Blog
  • Resources
  • Haptik v/s Yellow
  • Haptik v/s Liveperson
  • Haptik v/s IBM Watson
  • Haptik v/s Verloop
  • Conversations on AI

Company

  • Why Haptik
  • About Us
  • Careers
  • News & Media
  • Awards & Recognition
  • Contact Us
  • Partnerships
  • Investor Relations

Subscribe

Sign up to recieve the latest updates

Find us on

  • Twitter-footer
  • Linkedin-footer
  • YT-footer
  • Insta-footer
  • G2-footer
  • Facebook-footer

Knowledge Base Software powered by Helpjuice

Copyright © jio Haptik Technology Limited 2021 | Data Security & Privacy Policy | GDPR

North America | Asia Pacific | Africa | enterprise@haptik.ai

Definition by Author

0
0