Code Examples

In this section we provide some examples for some of the most popular programming languages.

📘

Legacy Endpoints

These examples work with any of the URLs shown at the beginning of the REST API Native connection section.

HTTP Client

POST https://apiv2.usemarvin.ai/naics/naics/search/additional/
Content-Type: application/json

{
  "token": "YOUR_TOKEN_HERE",
  "name": "Godbys Lawn Mower Shop Inc.",
  "address": "4133 Richardson Rd",
  "state": "KY"
}

Java

package com.relativity6.prediction;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Prediction {

    private static final Logger logger = Logger.getLogger(Prediction.class.getName());

    public static void main(String[] args) {

        // create the request body data
        var bodyData = "{" +
                "\"token\": \"YOUR_TOKEN_HERE\"," +
                "\"name\": \"Godbys Lawn Mower Shop Inc.\"," +
                "\"address\": \"4133 Richardson Rd\"," +
                "\"state\": \"KY\"" +
                "}";

        // create the http client
        var client = HttpClient.newHttpClient();

        // create the http request
        var request = HttpRequest.newBuilder()
                .uri(URI.create("https://apiv2.usemarvin.ai/naics/naics/search/additional/"))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(bodyData))
                .build();

        try {

            // send request to API
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            // show response in console
            logger.log(Level.INFO, "http request response: {}", response);
        }catch (InterruptedException interruptedException){

            // interrupted current thread
            Thread.currentThread().interrupt();

            // show exception in console
            logger.log(Level.INFO, "error - execution thread was interrupted", interruptedException);
        }catch(Exception e){

            // show exception in console
            logger.log(Level.INFO, "error - an error occur fetching prediction", e);
        }
    }
}

JavaScript

function prediction() {

    const url = "https://apiv2.usemarvin.ai/naics/naics/search/additional/"

    const data = {
        "token": "YOUR_TOKEN_HERE",
        "name": "Godbys Lawn Mower Shop Inc.",
        "address": "4133 Richardson Rd",
        "state": "KY"
    }

    fetch(url, { 
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(data)
        })
        .then(resp => {
             return resp.json()
             })
        .then(res =>
             console.log(res))
        .catch(err => 
            console.log(err)
            )
}

Python

import json
import requests


def prediction():
    url='https://apiv2.usemarvin.ai/naics/naics/search/additional/'
    values = {
        "token": "YOUR_TOKEN_HERE",
        "name": "Godbys Lawn Mower Shop Inc.",
        "address": "4133 Richardson Rd",
        "state": "KY"
    }
    headers = {'content-type': 'application/json'}

    response = requests.post(url, data=json.dumps(values), headers=headers)
    return response.json()

if __name__ == '__main__':
    print(prediction())

PHP

<?php 

$sUrl = "https://apiv2.usemarvin.ai/naics/naics/search/additional/";

$array = array(
    'token'=> 'YOUR_TOKEN_HERE',
    'name'=> 'Godbys Lawn Mower Shop Inc.',
    'address'=> '4133 Richardson Rd',
    'state'=> 'KY'
);


$myJson = json_encode($array);

$data = http_build_query($array);

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_URL, $sUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $myJson);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$resp = curl_exec($ch);

if (curl_error($ch)) 
{
    echo curl_error($ch);
}
else
{

    $decoded = json_decode($resp, true);
    $jsonResponse = json_encode(json_decode($resp),JSON_PRETTY_PRINT);

    echo '<pre>'.$jsonResponse.'</pre>';

} 
curl_close($ch);

?>

C#

using System;
using System.Net.Http;
using System.Json;
using System.Text;
using System.Threading.Tasks;

public class Program
{
    private static readonly HttpClient client = new HttpClient();

    public static async Task  Main()
    {
        
        var myObject = new JsonObject();
        
        myObject.Add("name", "Relativity6");
        myObject.Add("token", "YOUR_TOKEN_HERE");
        
        var content = new StringContent(myObject.ToString(), Encoding.UTF8, "application/json");

        try
        {
            var response = await client.PostAsync("https://apiv2.usemarvin.ai/naics/naics/search/additional/", content);
            
            var responseString = await response.Content.ReadAsStringAsync();
    
            Console.WriteLine(responseString);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }   
}

Example repositories

In order to make it easier for you, we have implemented some projects on Git ready to use.

Java Example Bitbucket

git clone [email protected]:r6team/java-sample-connection.git

JavaScript Example Bitbucket

git clone [email protected]:r6team/javascript-sample-connection.git

Python Example Bitbucket

git clone [email protected]:r6team/python-sample-connection.git

PHP Example Bitbucket

git clone [email protected]:r6team/php-sample-connection.git

C# .NET Example Bitbucket

git clone [email protected]:r6team/c-.net-sample-connection.git

What’s Next

Check out our list of potential error messages

Did this page help you?