Help articles

How to use Insomnia

TI provides all of our APIs as OpenAPI documents that can be imported into tools such as Insomnia. Insomnia can be downloaded for free at https://insomnia.rest/.   

Steps:

  1. Open Insomnia
  2. Create a new project
  3. Click on create > Request collection

 

 4. Import the TI OpenAPI file.

 5. Go to Manage environments.

  • Ensure that your base environment contains the following variable:
    • {
                                  "base_url": "{{ scheme }}://{{ host }}{{ base_path }}"
                                  }

 6. After updating the base environment select the transact.ti.com environment.

 7. Update "oauth2ClientId" with your TI store API key and "oauth2ClientSecret" with your TI store API secret.


How to use code snippets

Code snippets that can be used in your implementation can be generated in many different languages for TI APIs by using the OpenAPI specifications embedded in documentation of the TI API portal or using the Insomnia tool. 

Steps:

  1. Go to the TI API portal.
  2. Go to the desired API page.
  3. Expand one of the methods.
  4. Select "Try it out”.
  5. Select "Execute”.
  6. A tab page of code snippets will open up.

 

From Insomnia:
 

  1. After importing the collection into Insomnia, select the method and down arrow.
  2. Select "Generate Code".

 

VB.Net example

Framework 4.6.1

Imports System.Net
Imports System.IO
Imports System.Text
Imports Newtonsoft.Json.Linq


Public Class RESTAPI
    Public ClientID As String
    Public ClientSecret As String
    Private MyToken As RESTAPI.Token
    Public Function GetPricing(TiPartNumber As String) As String
        If IsNothing(MyToken) Then
            GetToken()
        Else
            If MyToken.IsValid = False Then
                GetToken()
            End If
        End If

        Return GetWithOAuth2($"https://transact.ti.com/v2/store/products/{TiPartNumber}?currency=USD&exclude-evms=true", MyToken.Token)


    End Function
    Private Sub GetToken()

        Dim URL As String = "https://transact.ti.com/v1/oauth/accesstoken"

        Dim Data As String = $"grant_type=client_credentials&client_id={ClientID }&client_secret={ClientSecret}"
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(URL), HttpWebRequest)
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"   
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(Data)
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        dataStream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim result As String = reader.ReadToEnd()
        reader.Close()
        dataStream.Close()
        response.Close()

        Dim parsejson As JObject = JObject.Parse(result)
        MyToken = New Token With {.Time = Now,
            .Token = parsejson.SelectToken("access_token").ToString
            }

    End Sub

    Public Function GetWithOAuth2(ByVal url As String, ByVal accessToken As String) As String
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        request.Method = "GET"
        request.Headers.Add("Authorization", "Bearer " + accessToken)
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Dim responseStream As Stream = response.GetResponseStream()
        Dim reader As New StreamReader(responseStream)
        Dim result As String = reader.ReadToEnd()
        reader.Close()
        response.Close()
        Return result
    End Function

    Private Class Token
        Public Token As String
        Public Time As DateTime
        Public Function IsValid() As Boolean
            If DateDiff(DateInterval.Minute, Now, Time) >= 60 Then
                Return False
            Else
                Return True
            End If
        End Function
    End Class

End Class