Brave Search 🦁

Privacy-focused web search with comprehensive results.

πŸ”

What you can do: Web search, news search, image search, video search, local business search, and AI-powered answer summarization - all with privacy-first design.

Setup

Add your Brave Search API key in the ProtectMyAPI Dashboard.


let brave = ProtectMyAPI.braveSearchService()
 
let results = try await brave.webSearch(
    query: "SwiftUI best practices 2024"
)
 
for result in results.web?.results ?? [] {
    print("Title: \(result.title)")
    print("URL: \(result.url)")
    print("Description: \(result.description ?? "")")
    print("---")
}

With Options

let results = try await brave.webSearch(
    query: "machine learning tutorials",
    options: BraveSearchOptions(
        count: 20,              // Number of results (max 20)
        offset: 0,              // Pagination offset
        country: "US",          // Country code
        searchLang: "en",       // Search language
        uiLang: "en-US",        // UI language
        safesearch: .moderate,  // Safe search filter
        freshness: .pastMonth,  // Time filter
        textDecorations: true,  // Include bold/italic markers
        spellcheck: true        // Auto-correct spelling
    )
)

Get current news articles:

let news = try await brave.newsSearch(
    query: "artificial intelligence",
    options: BraveSearchOptions(
        count: 10,
        freshness: .pastDay
    )
)
 
for article in news.results ?? [] {
    print("πŸ“° \(article.title)")
    print("Source: \(article.source ?? "Unknown")")
    print("Age: \(article.age ?? "")")
    print("URL: \(article.url)")
}

let images = try await brave.imageSearch(
    query: "cute cats",
    options: BraveImageSearchOptions(
        count: 20,
        safesearch: .strict,
        size: .large,         // small, medium, large, wallpaper
        imageType: .photo,    // photo, clipart, gif, transparent
        license: .any         // any, share, shareCommercially
    )
)
 
for image in images.results ?? [] {
    print("Image: \(image.title)")
    print("URL: \(image.url)")
    print("Thumbnail: \(image.thumbnail?.src ?? "")")
    print("Size: \(image.properties?.width ?? 0)x\(image.properties?.height ?? 0)")
}

let videos = try await brave.videoSearch(
    query: "Swift programming tutorial",
    options: BraveVideoSearchOptions(
        count: 10,
        freshness: .pastMonth,
        duration: .medium,    // short (<5min), medium (5-20min), long (>20min)
        resolution: .high     // low, medium, high
    )
)
 
for video in videos.results ?? [] {
    print("🎬 \(video.title)")
    print("Source: \(video.source ?? "Unknown")")
    print("Duration: \(video.duration ?? "")")
    print("Views: \(video.views ?? 0)")
    print("URL: \(video.url)")
}

Find local businesses and places:

let places = try await brave.localSearch(
    query: "coffee shops near me",
    location: BraveLocation(
        latitude: 37.7749,
        longitude: -122.4194,
        radius: 5000 // meters
    )
)
 
for place in places.results ?? [] {
    print("πŸ“ \(place.title)")
    print("Address: \(place.address ?? "N/A")")
    print("Rating: \(place.rating?.value ?? 0)/5 (\(place.rating?.count ?? 0) reviews)")
    print("Phone: \(place.phone ?? "N/A")")
    print("Hours: \(place.openingHours?.current ?? "Unknown")")
    if place.isOpen == true {
        print("βœ… Currently Open")
    }
}

AI Summarization

Get AI-generated summaries of search results:

let results = try await brave.webSearch(
    query: "What is quantum computing",
    options: BraveSearchOptions(
        summary: true // Enable AI summary
    )
)
 
// Get the AI-generated summary
if let summary = results.summarizer?.summary {
    print("Summary: \(summary)")
}
 
// Also get the source results
for result in results.web?.results ?? [] {
    print("- \(result.title): \(result.url)")
}

Suggest / Autocomplete

let suggestions = try await brave.suggest(query: "how to learn")
 
for suggestion in suggestions.results ?? [] {
    print("πŸ’‘ \(suggestion.query)")
}

Search Options

Freshness Filters

ValueDescription
.pastDayLast 24 hours
.pastWeekLast 7 days
.pastMonthLast 30 days
.pastYearLast 365 days
ValueDescription
.offNo filtering
.moderateFilter explicit content
.strictStrict filtering

Image Sizes

ValueDescription
.smallSmall images
.mediumMedium images
.largeLarge images
.wallpaperVery large/wallpaper

Image Types

ValueDescription
.photoPhotographs
.clipartClip art
.gifAnimated GIFs
.transparentTransparent background

Video Duration

ValueDescription
.shortLess than 5 minutes
.medium5-20 minutes
.longOver 20 minutes

Response Structure

Web Search Response

struct BraveWebResponse {
    let query: BraveQuery           // Original query info
    let web: BraveWebResults?       // Web results
    let news: BraveNewsResults?     // Related news
    let videos: BraveVideoResults?  // Related videos
    let summarizer: BraveSummary?   // AI summary (if enabled)
    let infobox: BraveInfobox?      // Knowledge panel
    let discussions: [BraveDiscussion]? // Forum discussions
}

Web Result

struct BraveWebResult {
    let title: String
    let url: String
    let description: String?
    let age: String?                // "2 hours ago", "3 days ago"
    let language: String?
    let familyFriendly: Bool?
    let thumbnail: BraveThumbnail?
    let extraSnippets: [String]?    // Additional text excerpts
}

Pricing

Brave Search API offers:

  • Free tier: 2,000 queries/month
  • Basic: $5/1,000 queries
  • Pro: Volume discounts available

Check their API pricing for current rates.