Pricing API v1からPricing API v2への移行方法
概要
このガイドでは、Pricing API v1 から Pricing API v2 への移行方法を説明します。Pricing API の新バージョンでは、認証、ルート、レスポンス構造が変更されました。このガイドでは、統合を更新するためにアプリケーションに必要な変更を説明します。
新しいAPIによって改善された一貫性、セキュリティ、柔軟性を活用するためには、Pricing API v2への移行が重要です。
始める前に
移行を開始する前に、以下を確認してください:
- 既存のVonage API KeyとAPI Secretをお持ちです。
- 新機能を理解するために、このドキュメントをお読みになったことでしょう。
- 本番環境にデプロイする前に、変更をテストするためのテスト環境や開発環境がある。
URLの変更に注意
Pricing API v2では、v1と比較してAPIのベースURLが新しくなっています。
アプリケーションで、ベースURLとルートを次のように変更してください。
https://rest.nexmo.com/account/get-pricing/outbound/へのhttps://api.nexmo.com/v2/account/pricing/// Example using the old route const apiKey = 'vonage_api_key'; // Replace with your API Key const apiSecret = 'vonage_api_secret'; // Replace with your API Secret const product = 'sms'; // Example product const country = 'CA'; // Example country code const url = `https://rest.nexmo.com/account/get-pricing/outbound/${product}?api_key=${apiKey}&api_secret=${apiSecret}&country=${country}`;// Example using the new route const apiKey = 'vonage_api_key'; // Replace with your API Key const apiSecret = 'vonage_api_secret'; // Replace with your API Secret const product = 'sms-outbound'; // Example product const country = 'CA'; // Example country code const url = `https://api.nexmo.com/v2/account/pricing/${product}`
適切な製品を選ぶ
Pricing API v2 がサポートしているのは、次のものだけです。 sms-outbound そして voice-outboundプライシングAPI v1では、次のような機能がサポートされていた。 sms, sms-transitそして voice.製品名は変更されるため、適切な製品名を呼ぶ必要があります。ほとんどの場合 sms-transit そして sms に変更できる。 sms-outboundそして voice に変更できる。 voice-outbound.
ベーシック・ヘッダ認証への移行
Pricing API v1ではクエリパラメータ認証を使用してリクエストの検証を行っていましたが、Pricing API v2では以下のようなBasic認証を使用するようになりました。 Authorization ヘッダーを使用する。
現在のアプリケーションでAPIリクエストを見つけてください。Node.jsを使用している場合、リクエストは以下のようになります:
const fetch = require('node-fetch'); const apiKey = 'vonage_api_key'; // Replace with your API Key const apiSecret = 'vonage_api_secret'; // Replace with your API Secret const product = 'sms'; // Example product const country = 'CA'; // Example country code const url = `https://rest.nexmo.com/account/get-pricing/outbound/${product}?api_key=${apiKey}&api_secret=${apiSecret}&country=${country}`; fetch(url) .then(response => { if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.json(); }) .then(data => { console.log('Pricing Data:', data); }) .catch(error => { console.error('Error fetching pricing data:', error); });const axios = require('axios'); const apiKey = 'vonage_api_key'; // Replace with your API Key const apiSecret = 'vonage_api_secret'; // Replace with your API Secret const product = 'sms-outbound'; // Example product const country = 'CA'; // Example country code const url = `https://rest.nexmo.com/account/get-pricing/outbound/sms`; axios .get(url, { params: { api_key: apiKey, api_secret: apiSecret, country: country, }, }) .then(response => { console.log('Pricing Data:', response.data); }) .catch(error => { console.error('Error fetching pricing data:', error.response ? error.response.data : error.message); });を取り外す。
api_keyそしてapi_secretパラメータに置き換えてください。AuthenticationヘッダはBasic認証を使用する。// Example using Node Fetch const fetch = require('node-fetch'); const apiKey = 'vonage_api_key'; // Replace with your API Key const apiSecret = 'vonage_api_secret'; // Replace with your API Secret const product = 'sms-outbound'; // Example product const country = 'CA'; // Example country code const url = `https://api.nexmo.com/v2/account/pricing/${product}?country=${country}`; fetch(url, { method: 'GET', headers: { Authorization: `Basic ${Buffer.from(`${apiKey}:${apiSecret}`).toString('base64')}`, }, }) .then(response => { if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.json(); }) .then(data => { console.log('Pricing Data:', data); }) .catch(error => { console.error('Error fetching pricing data:', error); });// Example using Axios const axios = require('axios'); const apiKey = 'vonage_api_key'; // Replace with your API Key const apiSecret = 'vonage_api_secret'; // Replace with your API Secret const product = 'sms-outbound'; // Example product const country = 'CA'; // Example country code const url = `https://api.nexmo.com/v2/account/pricing/${product}`; axios .get(url, { auth: { username: username, password: password, }, params: { country: country, }, }) .then(response => { console.log('Pricing Data:', response.data); }) .catch(error => { console.error('Error fetching pricing data:', error.response ? error.response.data : error.message); });
新対応体制への変更
Pricing API v2 では、次のようになりました。 JSON-HAL レスポンス構造を他の Vonage API とより整合性のあるものにしました。これは、Pricing API v1のフラットなレスポンス構造とは異なるAPIレスポンス構造ですが、結果のページングが改善されるなどの利点があります。
レスポンスのデータロケーションの変更に注意してください。価格とネットワーク情報は、現在
_embedded.countries配列の代わりにnetworks配列である。// Pricing API v1 Response { "countryCode": "CA", "countryName": "Canada", "countryDisplayName": "Canada", "currency": "EUR", "defaultPrice": "0.00620000", "dialingPrefix": "1", "networks": [ { "type": "mobile", "price": "0.00590000", "currency": "EUR", "mcc": "302", "mnc": "530", "networkCode": "302530", "networkName": "Keewaytinook Okimakanak" } ] }// Pricing API v2 Response { "page_size": "100", "page": "1", "total_items": "243", "total_pages": "3", "_embedded": { "countries": [ { "country_name": "Canada", "dialing_prefix": "1", "dest_network_type": "ALL", "group_internal_start": "1s", "rate_increment": "60", "currency": "USD", "price": "0.1" } ] }, "_links": { "self": { "href": "https://api.nexmo.com/account/pricing/sms-outbound?page=1&page_size=100" }, "first": { "href": "https://api.nexmo.com/account/pricing/sms-outbound?page=1&page_size=100" }, "last": { "href": "https://api.nexmo.com/account/pricing/sms-outbound?page=3&page_size=100" }, "next": { "href": "https://api.nexmo.com/account/pricing/sms-outbound?page=2&page_size=100" }, "prev": { "href": "https://api.nexmo.com/account/pricing/sms-outbound?page=1&page_size=100" } } }キー名の変更に注意してください。キー名には
snake_caseの代わりにcamelCase.これにより、VonageのAPI標準に準拠し、他のAPIレスポンスとの一貫性が保たれます。キーの位置も一部変更されています。
countryCodeがURLのcountryクエリ・パラメータ。countryNameは現在_embedded.countries[X].country_name.countryDisplayNameは現在_embedded.countries[X].country_name.currencyは現在_embedded.countries[X].currency.defaultPriceは現在_embedded.countries[X].price.dialingPrefixは現在_embedded.countries[X].dialing_prefix.network[X].typeは返されなくなった。network[X].priceは現在_embedded.countries[X].price.network[X].currencyは現在_embedded.countries[X].currency.network[X].mccは返されなくなった。network[X].mncは返されなくなった。network[X].networkCodeは返されなくなった。network[X].networkNameは返されなくなった。
- レスポンスはより正確にページングを反映するようになった。このため
pageキーをtotal_pagesキーで結果の末尾にいるかどうかを確認する。_links.nextキーを使用してください。次のページに移動したい場合は_links.next.hrefの値を自動的に決定し、次のページにナビゲートする。