导入
地址
学习如何创建、验证和获取 Shippo 地址。
我们必须提供“发件人”地址和收件人地址,并在发货前对这些地址进行验证,以避免发货失败。Shippo API 免费验证美国地址,而全球(非美国)地址在使用正式令牌时会收取费用。如果使用测试令牌,API 将不会对全球地址进行验证。
代码示例
1https://api.goshippo.com/addresses/
| 字段 | 类型 | 分类 | 说明 |
|---|---|---|---|
| name | string | 必填 | 收件人姓名。 |
| company | string | 可选 | 公司名称。 |
| street1 | string | 必填 | 街道地址第一行。 |
| street_no | string | 可选 | 街道门牌号。 |
| city | string | 必填 | 城市名称。 |
| zip | string | 必填 | 邮政编码。 |
| state | string | 必填 | 州名。部分国家要求此字段。 |
| country | string | 必填 | 国家名称,需使用 ISO 2 国家代码。 |
| phone | string | 可选 | 联系电话。 |
| string | 可选 | 电子邮箱地址。 | |
| is_residential | boolean | 可选 | 标识地址是住宅还是商业用途。 |
| validate | boolean | 可选 | 是否启用地址验证。 |
1// 接口地址 2const endpointUrl = "https://api.goshippo.com/addresses/"; 3 4// 请求头参数 5const headerParameters = { 6 "authorization": "ShippoToken {{TOKEN}}", 7 "Content-Type": "application/json", 8}; 9 10// 请求体参数 11const bodyParameters = JSON.stringify({ 12 name: "Shawn Ippotle", 13 company: "Shippo", 14 street1: "215 Clayton St.", 15 city: "San Francisco", 16 state: "CA", 17 zip: "94117", 18 country: "US", 19 email: "example@goshippo.com" 20}); 21 22// 设置请求选项 23const options = { 24 method: "POST", 25 headers: headerParameters, 26 body: bodyParameters, 27}; 28 29// 发送 API 请求的函数 30async function createAddress() { 31 try { 32 const response = await fetch(endpointUrl, options); 33 // 输出响应结果 34 printResponse(response); 35 } catch (error) { 36 // 输出错误信息 37 printError(error); 38 } 39} 40 41// 调用函数发送请求 42createAddress();
1// 请求体参数 2const bodyParameters = JSON.stringify({ 3 name: "Mrs. Hippo", 4 street1: "965 Mission St.", 5 city: "San Francisco", 6 state: "CA", 7 zip: "94105", 8 country: "US" 9});
| 字段 | 类型 | 说明 |
|---|---|---|
| object_id | string | 新创建地址对象的唯一标识 ID。 |
| is_complete | boolean | 表示地址信息是否完整。 |
| is_residential | boolean | 若启用验证,API 可自动判断地址为住宅或商业;也可手动传入该标志。否则值为 null。 |
| test | boolean | 表示当前使用的是测试令牌还是正式令牌。 |
| validation_results | object | 启用验证后,API 返回验证结果对象,包含验证状态(is_valid)和提示信息(message)。若地址有效,is_valid 为 true,message 为空;若无效,is_valid 为 false,message 会说明失败原因。 |
| zip | string | 启用验证时,API 会校验邮编并返回修正后的值(如原始输入不准确)。 |
| latitude | number | 地址的纬度坐标,由 API 根据地址自动计算。 |
| longitude | number | 地址的经度坐标,由 API 根据地址自动计算。 |
代码示例
1https://api.goshippo.com/addresses/{ADDRESS_OBJECT_ID}/validate/
1// 定义接口地址 2const endpointUrl = "https://api.goshippo.com/addresses/{{ADDRESS_OBJECT_ID}}/validate/"; 3 4// 定义请求头参数 5const headerParameters = { 6 "authorization": "ShippoToken {{TOKEN}}" 7}; 8 9// 设置请求选项 10const options = { 11 method: "GET", 12 headers: headerParameters, 13}; 14 15// 发送验证请求的函数 16async function validateAddress() { 17 try { 18 const response = await fetch(endpointUrl, options); 19 // 输出响应结果 20 printResponse(response); 21 } catch (error) { 22 // 输出错误信息 23 printError(error); 24 } 25} 26 27// 调用函数发送请求 28validateAddress();
代码示例
1https://api.goshippo.com/addresses/
1// 定义接口地址 2const endpointUrl = "https://api.goshippo.com/addresses/"; 3 4// 定义请求头参数 5const headerParameters = { 6 "authorization": "ShippoToken {{TOKEN}}" 7}; 8 9// 设置请求选项 10const options = { 11 method: "GET", 12 headers: headerParameters, 13}; 14 15// 获取所有地址列表的函数 16async function listAllAddresses() { 17 try { 18 const response = await fetch(endpointUrl, options); 19 // 输出响应结果 20 printResponse(response); 21 } catch (error) { 22 // 输出错误信息 23 printError(error); 24 } 25} 26 27// 调用函数发送请求 28listAllAddresses();
代码示例
1https://api.goshippo.com/addresses/{ADDRESS_OBJECT_ID}
1// 定义接口地址 2const endpointUrl = "https://api.goshippo.com/addresses/{{ADDRESS_OBJECT_ID}}"; 3 4// 定义请求头参数 5const headerParameters = { 6 "authorization": "ShippoToken {{TOKEN}}" 7}; 8 9// 设置请求选项 10const options = { 11 method: "GET", 12 headers: headerParameters, 13}; 14 15// 获取指定地址详情的函数 16async function retrieveSpecificAddress() { 17 try { 18 const response = await fetch(endpointUrl, options); 19 // 输出响应结果 20 printResponse(response); 21 } catch (error) { 22 // 输出错误信息 23 printError(error); 24 } 25} 26 27// 调用函数发送请求 28retrieveSpecificAddress();