Classic/VPC環境で利用できます。
ショッピングインサイト APIを使用して、ショッピング分野別の検索クリックのトレンドを照会する実装例です。ショッピングインサイト APIの他の操作の実装方法もこれと類似しているため、この実装例を参照すれば、ショッピングインサイト APIを実装することができます。
参考
- サンプルコードの
YOUR_CLIENT_IDには、NAVERクラウドプラットフォームコンソールで発行された Client IDを、YOUR_CLIENT_SECRETには、その Client IDとマッピングする Client Secretを入力してください。 - Client IDと Client Secretの発行方法は、NAVER API HUBの概要をご参照ください。
分野別トレンドの照会
NAVER統合検索のショッピングエリアおよび NAVERショッピングにおける検索クリック数の推移を、ショッピング分野別に照会するショッピングインサイト APIのサンプルコードを説明します。リクエストボディのcategoryと検索条件(device、ages、genderなど)を別の値に置き換えることで、同様の方法で他の分野のクリック数の推移を照会することができます。
Java
Javaベースの Shopping Insight APIのサンプルコードは次の通りです。
package com.naver.apihub.example;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class ApiExampleShoppingCategories {
public static void main(String[] args) {
String clientId = "YOUR_CLIENT_ID"; // アプリケーションの Client ID
String clientSecret = "YOUR_CLIENT_SECRET"; // アプリケーションの Client Secret
String apiUrl = "https://naverapihub.apigw.ntruss.com/shopping/v1/categories";
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("X-NCP-APIGW-API-KEY-ID", clientId);
requestHeaders.put("X-NCP-APIGW-API-KEY", clientSecret);
requestHeaders.put("Content-Type", "application/json");
String requestBody = "{\"startDate\":\"2026-01-01\"," +
"\"endDate\":\"2026-05-31\"," +
"\"timeUnit\":\"month\"," +
"\"category\":[{\"name\":\"ファッション・アパレル\",\"param\":[\"50000000\"]}," +
"{\"name\":\"化粧品/美容\",\"param\":[\"50000002\"]}]," +
"\"device\":\"pc\"," +
"\"gender\":\"f\"," +
"\"ages\":[\"20\",\"30\"]}";
String responseBody = post(apiUrl, requestHeaders, requestBody);
System.out.println(responseBody);
}
private static String post(String apiUrl, Map<String, String> requestHeaders, String requestBody) {
HttpURLConnection con = connect(apiUrl);
try {
con.setRequestMethod("POST");
for (Map.Entry<String, String> header : requestHeaders.entrySet()) {
con.setRequestProperty(header.getKey(), header.getValue());
}
con.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
wr.write(requestBody.getBytes(StandardCharsets.UTF_8));
wr.flush();
}
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // 正常にレスポンス
return readBody(con.getInputStream());
} else { // エラーを返す
return readBody(con.getErrorStream());
}
} catch (IOException e) {
throw new RuntimeException("APIリクエストおよびレスポンス失敗", e);
} finally {
con.disconnect();
}
}
private static HttpURLConnection connect(String apiUrl) {
try {
URL url = new URL(apiUrl);
return (HttpURLConnection) url.openConnection();
} catch (MalformedURLException e) {
throw new RuntimeException("無効な API URLです。: " + apiUrl, e);
} catch (IOException e) {
throw new RuntimeException("接続できません。: " + apiUrl, e);
}
}
private static String readBody(InputStream body) {
InputStreamReader streamReader = new InputStreamReader(body, StandardCharsets.UTF_8);
try (BufferedReader lineReader = new BufferedReader(streamReader)) {
StringBuilder responseBody = new StringBuilder();
String line;
while ((line = lineReader.readLine()) != null) {
responseBody.append(line);
}
return responseBody.toString();
} catch (IOException e) {
throw new RuntimeException("APIレスポンスを読み取れません。", e);
}
}
}
PHP
PHPベースの Shopping Insight APIのサンプルコードは次の通りです。
<?php
$client_id = "YOUR_CLIENT_ID";
$client_secret = "YOUR_CLIENT_SECRET";
$url = "https://naverapihub.apigw.ntruss.com/shopping/v1/categories";
$body = "{\"startDate\":\"2026-01-01\",\"endDate\":\"2026-05-31\",\"timeUnit\":\"month\",\"category\":[{\"name\":\"ファッション・アパレル\",\"param\":[\"50000000\"]},{\"name\":\"化粧品/美容\",\"param\":[\"50000002\"]}],\"device\":\"pc\",\"gender\":\"f\",\"ages\":[\"20\",\"30\"]}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = "X-NCP-APIGW-API-KEY-ID: " . $client_id;
$headers[] = "X-NCP-APIGW-API-KEY: " . $client_secret;
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status_code == 200) {
echo $response;
} else {
echo "Errorの内容:" . $response;
}
?>
Node.js
Node.jsベースの Shopping Insight APIのサンプルコードは次の通りです。
const client_id = 'YOUR_CLIENT_ID';
const client_secret = 'YOUR_CLIENT_SECRET';
const api_url = 'https://naverapihub.apigw.ntruss.com/shopping/v1/categories';
const request_body = {
startDate: '2026-01-01',
endDate: '2026-05-31',
timeUnit: 'month',
category: [
{ name: 'ファッション・アパレル', param: ['50000000'] },
{ name: '化粧品/美容', param: ['50000002'] },
],
device: 'pc',
gender: 'f',
ages: ['20', '30'],
};
(async () => {
const response = await fetch(api_url, {
method: 'POST',
headers: {
'X-NCP-APIGW-API-KEY-ID': client_id,
'X-NCP-APIGW-API-KEY': client_secret,
'Content-Type': 'application/json',
},
body: JSON.stringify(request_body),
});
console.log(response.status);
console.log(await response.text());
})();
Python
Pythonベースの Shopping Insight APIのサンプルコードは次の通りです。
import urllib.request
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
url = "https://naverapihub.apigw.ntruss.com/shopping/v1/categories"
body = "{\"startDate\":\"2026-01-01\",\"endDate\":\"2026-05-31\",\"timeUnit\":\"month\",\"category\":[{\"name\":\"ファッション・アパレル\",\"param\":[\"50000000\"]},{\"name\":\"化粧品/美容\",\"param\":[\"50000002\"]}],\"device\":\"pc\",\"gender\":\"f\",\"ages\":[\"20\",\"30\"]}"
request = urllib.request.Request(url)
request.add_header("X-NCP-APIGW-API-KEY-ID", client_id)
request.add_header("X-NCP-APIGW-API-KEY", client_secret)
request.add_header("Content-Type", "application/json")
response = urllib.request.urlopen(request, data=body.encode("utf-8"))
rescode = response.getcode()
if rescode == 200:
response_body = response.read()
print(response_body.decode("utf-8"))
else:
print("Error Code:" + str(rescode))
C#
C# ベースの Shopping Insight APIのサンプルコードは次の通りです。
using System;
using System.IO;
using System.Net;
using System.Text;
namespace NaverApiHubExample
{
public class ApiExampleShoppingCategories
{
static void Main(string[] args)
{
string clientId = "YOUR_CLIENT_ID";
string clientSecret = "YOUR_CLIENT_SECRET";
string url = "https://naverapihub.apigw.ntruss.com/shopping/v1/categories";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("X-NCP-APIGW-API-KEY-ID", clientId);
request.Headers.Add("X-NCP-APIGW-API-KEY", clientSecret);
request.ContentType = "application/json";
request.Method = "POST";
string body = "{\"startDate\":\"2026-01-01\",\"endDate\":\"2026-05-31\",\"timeUnit\":\"month\",\"category\":[{\"name\":\"ファッション・アパレル\",\"param\":[\"50000000\"]},{\"name\":\"化粧品/美容\",\"param\":[\"50000002\"]}],\"device\":\"pc\",\"gender\":\"f\",\"ages\":[\"20\",\"30\"]}";
byte[] byteData = Encoding.UTF8.GetBytes(body);
request.ContentLength = byteData.Length;
using (Stream st = request.GetRequestStream())
{
st.Write(byteData, 0, byteData.Length);
}
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
Console.WriteLine(reader.ReadToEnd());
}
}
catch (WebException e) // エラーを返す
{
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream(), Encoding.UTF8))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}
}
}