# B2B API Integration Guide

This guide explains how to perform integration testing using your issued B2B API key (e.g., `mf_live_d16331489815f4c4b7b5a42c3048db71`).

You can easily send requests to the local development environment (`https://medifact.today`) or the production server endpoint.

---

## 1. cURL Command (Instant Terminal Test)
Open your terminal and run the command below to receive the fact-checking analysis results in JSON format.
(Please insert your actual key value in place of `mf_live_d16331489815f4c4b7b5a42c3048db71` or `Bearer mf_live_d16331489815f4c4b7b5a42c3048db71` in the `Authorization` header.)

```bash
curl -X POST https://medifact.today/api/v1/b2b/verify \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer mf_live_d16331489815f4c4b7b5a42c3048db71" \
  -d '{
    "text": "Regular intake of lutein and zeaxanthin maintains macular pigment density, which is proven to prevent macular degeneration.",
    "force_refresh": false
  }'
```

---

## 2. Postman or Insomnia API Client Test
* **Method**: `POST`
* **URL**: `https://medifact.today/api/v1/b2b/verify` *(or your production server domain)*
* **Headers**:
  * `Content-Type`: `application/json`
  * `Authorization`: `Bearer mf_live_d16331489815f4c4b7b5a42c3048db71`
* **Body (raw JSON)**:
  ```json
  {
    "text": "Regular intake of lutein and zeaxanthin maintains macular pigment density, which is proven to prevent macular degeneration.",
    "force_refresh": false
  }
  ```

---

## 3. Client Integration Examples

### Node.js (JavaScript - Fetch API)
This is an example for integrating within JavaScript-based backend services (NestJS, Express, etc.).

```javascript
const apiKey = "mf_live_d16331489815f4c4b7b5a42c3048db71"; // Your issued B2B API Key

async function verifyMedicalText(text) {
  try {
    const response = await fetch("https://medifact.today/api/v1/b2b/verify", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${apiKey}`
      },
      body: JSON.stringify({ text, force_refresh: false })
    });

    const result = await response.json();
    console.log("Verification confidence score:", result.medifact_score);
    console.log("Evidence status:", result.evidence_status);
    console.log("Evidence valid until:", result.valid_until);
    console.log("Detailed verification results:", JSON.stringify(result.results, null, 2));
  } catch (error) {
    console.error("API integration error:", error);
  }
}

verifyMedicalText("Collagen intake is highly effective for improving wrinkles in the dermis layer of the skin.");
```

### Ruby (Net::HTTP)
This is an example for integrating within Ruby or other Rails projects.

```ruby
require 'net/http'
require 'uri'
require 'json'

api_key = "mf_live_d16331489815f4c4b7b5a42c3048db71" # Your issued B2B API Key
uri = URI.parse("https://medifact.today/api/v1/b2b/verify")

header = {
  'Content-Type' => 'application/json',
  'Authorization' => "Bearer #{api_key}"
}
body = {
  text: "Fenbendazole, an animal dewormer, has anti-cancer effects.",
  force_refresh: false
}

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, header)
request.body = body.to_json

response = http.request(request)

if response.code == "200"
  result = JSON.parse(response.body)
  puts "Score: #{result['medifact_score']}"
  puts "Evidence status: #{result['evidence_status']}"
  puts "Medical Evidence: #{result['results']}"
else
  puts "Request failed: #{response.code} - #{response.body}"
end
```

---

## 4. B2B Sandbox Console Comparison
If you encounter integration issues or need to compare response schemas, you can enter claims into the **Live API Request Tester** on the [B2B API Sandbox page](https://medifact.today/b2b-demo) at any time. The sandbox terminal shows the exact JSON output that your code receives, allowing for easy cross-comparison.

## 5. Evidence Cache and Response Metadata

- `force_refresh` is an optional Boolean. When `true`, Medifact bypasses a fresh cache entry and retrieves evidence again.
- `evidence_status` is one of `fresh`, `updated`, `unchanged`, `stale_refresh_failed`, or `no_claims`.
- Normal claim-analysis responses include `evidence_checked_at`, `valid_until`, and `pipeline_version`. These fields may be absent for `no_claims`.
- The default freshness window is 24 hours, shortened to 6 hours when any claim is high-risk. Cache namespaces are isolated per B2B tenant.
- API keys with Zero-Data Retention enabled neither read nor write the result cache and do not create persistent fact-check records.

See the [Fact-check Workflow Operating Standard](fact_check_workflow_en.md) for the complete processing rules.
