Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

I want to make an API call to external application using scriptrunner

Aditi Nandagudi
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 5, 2024

Hi community, 

Is it possible to make an API call from script runner using behaviors?

My requirement is I have to send data entered on the create screen, in my case summary and description and make an API call to external application and show the results received from API on the screen in a third field? 

I am new to Script runner and not sure if this is possible 

1 answer

0 votes
Vikrant Yadav
Community Champion
June 5, 2024

Hi @Aditi Nandagudi Welcome to Atlassian Community!

Yes, you can send HTTP API request via Scriptrunner Behaviour. Here is the sample Script to send API request fromScriptrunner : 

import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.util.EntityUtils

def url = 'https://api.openai.com/v1/chat/completions'
def apiKey = 'sk-j4qZQqwS5LbkFJbALfxVk99xgsfiJmTlm4'
def prompt = 'Hello, ChatGPT!'
def requestBody = """{"model": "gpt-3.5-turbo","messages": [{"role": "user", "content": "What is jira"}]}"""

HttpClient client = HttpClientBuilder.create().build()
HttpPost post = new HttpPost(url)
post.setHeader('Content-Type', 'application/json')
post.setHeader('Authorization', 'Bearer ' + apiKey)
post.setEntity(new StringEntity(requestBody))

HttpResponse response = client.execute(post)
//return response


String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8")
//println(responseBody)
return responseBody

 

Parse Response : 


import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.util.EntityUtils
import groovy.json.JsonSlurper

def url = 'https://api.openai.com/v1/chat/completions'
def apiKey = 'sk-j4qZQqwSLfxVk99xgsfiJmTlm4'
def prompt = 'Hello, ChatGPT!'
def requestBody = """{"model": "gpt-3.5-turbo","messages": [{"role": "user", "content": "What is JIRA"}]}"""

HttpClient client = HttpClientBuilder.create().build()
HttpPost post = new HttpPost(url)
post.setHeader('Content-Type', 'application/json')
post.setHeader('Authorization', 'Bearer ' + apiKey)
post.setEntity(new StringEntity(requestBody))

HttpResponse response = client.execute(post)
println "Response status code: ${response.statusLine.statusCode}"
//def responseBody = EntityUtils.toString(response.entity)
//return "Response body: ${responseBody}"
def responseBody = response.getEntity().getContent().getText()
def jsonSlurper = new JsonSlurper()
def responseJson = jsonSlurper.parseText(responseBody)
def content = responseJson.choices[0].message.content
return content

//return response

It would be easy for you to use Project Automation to send HTTP API and put value in third field using Issue Created trigger in Project Automation. Project automation  easy to implement and low-code solution. If scriptrunner behaviour doesn't work for you, try Automation for Jira. 

 

Suggest an answer

Log in or Sign up to answer