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.
Web Search
Basic Search
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
)
)News Search
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)")
}Image Search
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)")
}Video Search
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)")
}Local Business Search
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
| Value | Description |
|---|---|
.pastDay | Last 24 hours |
.pastWeek | Last 7 days |
.pastMonth | Last 30 days |
.pastYear | Last 365 days |
Safe Search
| Value | Description |
|---|---|
.off | No filtering |
.moderate | Filter explicit content |
.strict | Strict filtering |
Image Sizes
| Value | Description |
|---|---|
.small | Small images |
.medium | Medium images |
.large | Large images |
.wallpaper | Very large/wallpaper |
Image Types
| Value | Description |
|---|---|
.photo | Photographs |
.clipart | Clip art |
.gif | Animated GIFs |
.transparent | Transparent background |
Video Duration
| Value | Description |
|---|---|
.short | Less than 5 minutes |
.medium | 5-20 minutes |
.long | Over 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.