# B2B API Integration Guide

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

You can easily send requests to the local development environment (`https://staging.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_06c59b2a8b20db064c3f76e5c9e4ed9b` or `Bearer mf_live_06c59b2a8b20db064c3f76e5c9e4ed9b` in the `Authorization` header.)

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

---

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

---

## 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_06c59b2a8b20db064c3f76e5c9e4ed9b"; // Your issued B2B API Key

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

    const result = await response.json();
    console.log("Verification confidence score:", result.medifact_score);
    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_06c59b2a8b20db064c3f76e5c9e4ed9b" # Your issued B2B API Key
uri = URI.parse("https://staging.medifact.today/b2b-demo/test-api")

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

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 "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://staging.medifact.today/b2b-demo) at any time. The sandbox terminal shows the exact JSON output that your code receives, allowing for easy cross-comparison.
